why p->member ?

Jerry Aguirre jerry at olivey.olivetti.com
Thu Aug 11 12:52:32 AEST 1988


In article <16734 at adm.ARPA> stanonik at nprdc.arpa (Ron Stanonik) writes:
>While explaining pointers to structures someone asked why
>the -> operator was needed; ie, why couldn't the members be
>referenced as p.member.

Now that everyone has explained what the difference between p. and p->
is and what they each mean (which he alread knew), how about answering
the question?

As a precedent the compiler already handles this type of conversion for
arrays.  I can declare:

	char b1[10];
	char *b2;

and then later refer to both of them as:
	
	b1[2] = b2[2];

Now one of these is the actual array and the other is a pointer.  The
compiler has to generate the same type of derefferencing that it would
for a structure refference.

I have had real trouble trying to explain this difference to novice C
programmers.  In particular the difference between:

	extern char *b1;
    and
	extern char b1[];

seems to confuse anyone used to a higher level language.  Many will
insist that the manual says they mean the same thing.

Getting back to structures though.  The final point is that if the
compiler can tell the difference then it is one less detail for the
programmer to worry about.  As long as old code would still work then
there should be no problem, right?
				Jerry Aguirre



More information about the Comp.unix.questions mailing list