char ***pointer;

Ray Butterworth rbutterworth at watmath.waterloo.edu
Sat Nov 19 02:31:04 AEST 1988


In article <1854 at xyzzy.UUCP>, throopw at 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.



More information about the Comp.lang.c mailing list