Xref: utzoo comp.lang.c:14163 comp.sys.ibm.pc:21356 Path: utzoo!utgpu!watmath!rbutterworth From: rbutterworth@watmath.waterloo.edu (Ray Butterworth) Newsgroups: comp.lang.c,comp.sys.ibm.pc Subject: Re: char ***pointer; Message-ID: <22258@watmath.waterloo.edu> Date: 18 Nov 88 16:31:04 GMT References: <360@mjbtn.MFEE.TN.US> <1854@xyzzy.UUCP> Organization: U of Waterloo, Ontario Lines: 49 In article <1854@xyzzy.UUCP>, throopw@xyzzy.UUCP (Wayne A. Throop) writes: > This IS one of those topics that comes by once every few months, so > there is clearly a lot of confusion about how arrays and pointers > relate to each other in C. > Hope this posting is of some help. One additional suggestion for people that are trying to understand the difference between pointers and arrays is to write some small test programs that print the results of sizeof() on the various variables and expressions. Some of the values will be surprising and until one understands why the values are what they are, one will never properly understand how to use arrays and pointers. Also, play around with assigning various expressions to variables and try to understand why something is or is not legal. e.g. main(){ /* using different prime dimensions helps when looking at sizeof() */ auto char arr[3][5][7]; auto char ***p; auto char (*q)[7]; auto char *r; p = arr; /* this is illegal */ q = &arr[0][0]; /* this is legal, but old compilers ignore the & */ r = &arr[0][0][0]; printf("%d %d %d %d\n", sizeof(arr), sizeof(arr[0]), sizeof(arr[0][0]), sizeof(arr[0][0][0])); printf("%d %d %d %d\n", sizeof(p), sizeof(*p), sizeof(**p), sizeof(***p)); printf("%d %d %d\n", sizeof(q), sizeof(*q), sizeof(*q[0])); printf("%d %d %d\n", sizeof(r), sizeof(*r), sizeof(r[0])); /* %d should be %ld on some systems */ } Note that the sizeof(arr), sizeof(p), etc. indicate the total number of bytes that are actually allocated by the auto declarations. So, for instance "auto char ***p" only allocates something like 4 bytes, even though p may eventually end up pointing at memory containing thousands of bytes. The 4 should indicate that the compiler has not allocated that other memory and it is still up to the programmer to find it somewhere.