Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!ames!sun-barr!apple!rutgers!att!westmark!mole-end!mat From: mat@mole-end.UUCP (Mark A Terribile) Newsgroups: comp.lang.c Subject: Re: Pointers, Structs, and Arrays Summary: *.[0], yes, of course ... Message-ID: <181@mole-end.UUCP> Date: 11 May 89 11:00:27 GMT References: <743@mccc.UUCP> Distribution: usa Organization: mole-end--private system. admin: mole-end!newtnews Lines: 40 In article <743@mccc.UUCP>, pjh@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