why p->member ?

Chuck Karish karish at denali.stanford.edu
Fri Aug 5 02:10:24 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.

This is explained in p. 122 of K&R, first edition.
Briefly, if we define a structure pointer with

	struct date { int year, month, day } *pd;

then

	pd->year

is equivalent to

	(*pd).year.

The parentheses show that we're dereferencing pd, not year.
Interpretation of the sort you discuss, in which the compiler
figures out automatically that p is a pointer and decides
on its own to dereference it, would be pretty confusing, not
to mention inconsistent with the way other pointers are handled.
Anyway, it's not part of the language.

Chuck Karish	ARPA:	karish at denali.stanford.edu
		BITNET:	karish%denali at forsythe.stanford.edu
		UUCP:	{decvax,hplabs!hpda}!mindcrf!karish
		USPS:	1825 California St. #5   Mountain View, CA 94041



More information about the Comp.unix.questions mailing list