Pointers, Structs, and Arrays
Mark A Terribile
mat at mole-end.UUCP
Thu May 11 21:00:27 AEST 1989
In article <743 at mccc.UUCP>, pjh at mccc.UUCP (Pete Holsberg) writes:
> I ran across the following code and, although I've figured out what it does,
> I'm not sure how to explain the notation.
> struct foo { char bar[20]; } list[100];
> init()
> {
> int i;
> for (i=0; i<100; ++i)
> *list[i].bar = '\0';
> }
> Now I know that the last line points to the first character in the bar array
> In each struct variable, but how? If bar = &bar[0], how so the '*' and the
> '&' "get together" to make the last line equivalent to
> list[i].bar[0]
It's really simple. Remember first that ``.'' and ``[]'' are what K&R-I call
``primary'' operators; they bind more tightly than anything else.
list[ i ].bar
is an array of char. Used as an expression, it is ``converted syntactically''
(it is taken to mean) a pointer-to-char pointing at the first element in the
array. What array? The array in the struct. Ok, then why do we write
*list[ i ].bar
instead of
list[ i ].*bar
Because the other operators are primaries, and it is the *entire* expression
from ``list'' to ``bar'' that is the pointer-to-char. The expression with
the ``*'' in the middle isn't legal C, though it can be legal C++.
--
(This man's opinions are his own.)
>From mole-end Mark Terribile
More information about the Comp.lang.c
mailing list