Path: utzoo!mnetor!uunet!husc6!rutgers!mcnc!rti!xyzzy!throopw From: throopw@xyzzy.UUCP (Wayne A. Throop) Newsgroups: comp.lang.c Subject: Re: Question about type casting Message-ID: <561@xyzzy.UUCP> Date: 17 Jan 88 19:22:25 GMT References: <5080009@hpfcdc.HP.COM> <5080010@hpfcdc.HP.COM> Organization: Data General, RTP NC. Lines: 45 > boemker@hpfcdc.HP.COM (Tim Boemker) > I want to know why a syntactically > correct and semantically meaningful expression is not allowed. > If you need further provocation, consider this program: > main() > { > int A[1]; > ( (int [1]) A ) [0] = 0; > } The question is ill formed. The above cast, while it *is* syntactically correct, is *NOT* semantically meaningful. The real question is why were the rules that give C syntax its semantics chosen in such a way as to make that cast meaningless. For a full answer, one would have to ask dmr, but the paraphrase of the question Tim asks can be addressed: > Why can't I cast an array as an array? To review the rules: casts take the value of the target expression and convert this value to a new type. An array, when it appears in an expression, evaluates (yields a value) with pointer type, not array type. Thus, the cast of what appears to be something to its own type is, in fact, an attempt to convert a pointer to an array, not an attempt to cast an array as an array. Now, why were these rules chosen? Well, the rule that really gets in the way here is the rule about how arrays are to be treated when evaluated. This special treatment of arrays in its turn was based on the unification of array subscripting and address arithmetic in C. In other words, it is the indirect consequence of an attempt to make C a simpler and more conceptually unified language. In retrospect, I think better tradeoffs could have been made, but I don't myself pretend to be a genius language designer, so I tend to accept C as it is, with only modest regrets. -- Giving up on assembly language was the apple in our Garden of Eden: Languages whose use squanders machine cycles are sinful. The LISP machine now permits LISP programmers to abandon bra and fig-leaf. --- Alan J. Perlis Moon calls them Fortran Machines and Lisp Machines, but I would prefer to call them fuzzy mammals and pterodactyls myself. --- Richard P. Gabriel -- Wayne Throop !mcnc!rti!xyzzy!throopw