[comp.lang.c] Pointers, Structs, and Arrays

pjh@mccc.UUCP (Pete Holsberg) (05/09/89)

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]
??
-- 
Pete Holsberg                   UUCP: {...!rutgers!}princeton!mccc!pjh
Mercer College				CompuServe: 70240,334
1200 Old Trenton Road           GEnie: PJHOLSBERG
Trenton, NJ 08690               Voice: 1-609-586-4800

ark@alice.UUCP (Andrew Koenig) (05/10/89)

In article <743@mccc.UUCP>, pjh@mccc.UUCP (Pete Holsberg) writes:

> 		*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]

Easy ... `.' binds more tightly than any unary operator, so

		*list[i].bar

is equivalent to

		*(list[i].bar)
-- 
				--Andrew Koenig
				  ark@europa.att.com

scjones@sdrc.UUCP (Larry Jones) (05/11/89)

In article <743@mccc.UUCP>, pjh@mccc.UUCP (Pete Holsberg) writes:
> struct foo
> {
> 	char bar[20];
> } list[100];
> 
> 		*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]

Well, the problem is that bar is NOT equivalent to &bar[0].
Rather, list[i].bar is equivalent to &list[i].bar[0], which
should make it easier to see how the "*" and the "&" get
together.

What you have to realize is that bar DOES NOT EXIST -- since it
is a member of a structure, it only exists as part of an actual
instance of the structure and must be referenced as such.
----
Larry Jones                         UUCP: uunet!sdrc!scjones
SDRC                                      scjones@SDRC.UU.NET
2000 Eastman Dr.                    BIX:  ltl
Milford, OH  45150-2789             AT&T: (513) 576-2070
"You can't get a body like mine in a bottle --
unless you push REAL HARD." - Judy Tenuta / Dr. Pepper

mat@mole-end.UUCP (Mark A Terribile) (05/11/89)

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