Path: utzoo!utgpu!water!watmath!clyde!rutgers!sri-spam!ames!pasteur!ucbvax!hplabs!hpcea!hpfcdc!boemker From: boemker@hpfcdc.HP.COM (Tim Boemker) Newsgroups: comp.lang.c Subject: Re: Question about type casting Message-ID: <5080010@hpfcdc.HP.COM> Date: 12 Jan 88 23:29:56 GMT References: <5080009@hpfcdc.HP.COM> Organization: HP Ft. Collins, Co. Lines: 37 > In article <5080009@hpfcdc.HP.COM> boemker@hpfcdc.HP.COM (Tim Boemker) writes: > -What's wrong with this program? > -main() > -{ > - int *i; > - ((int [1]) i) [0] = 1; > -} > > How can you turn a pointer into an array? It makes no sense. > Just use i[0] = 1; which is the same as *i = 1;. Consider int A[1]. A is semantically an array, but the expression "A" is a pointer to the base of that array. The only differences between a pointer and array are (1) when the storage is allocated and (2) how index calculations are made. Using i[i] instead of *i may be fine if the array in question is one dimensional, but it doesn't work exactly the same way when the array is multidimensional. Consider int *A. If I know that A points to a 10x20 array, I might like to address elements as A[i][j]. I can do the indexing math myself, as in *(A + i * 20 + j), but that's not what the question is really about. 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; } My C compiler doesn't like that construction, either, presumably for the same reason that it didn't like the one in the first example. But please explain to me why it's illegal to cast an expression into it's natural type! Why can't I cast an array as an array?