why p->member ?

Randal L. Schwartz @ Stonehenge merlyn at intelob.intel.com
Wed Aug 10 02:33:26 AEST 1988


In article <474 at sp7040.UUCP>, jsp at sp7040 (John Peters) writes:
| 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.
| 
| It needs to be referenced as a pointer.  To do that use:
| 
| 		*p.member
| 
| it is the same as
| 
| 		p->member

Whup.  Not quite.  *p.member is *(p.member).  Try (*p).member instead.
That's one reason the -> syntax was introduced (from what I remember
in reading the white Bible) -- to prevent having to do a lot of parens
for a fairly common operation.  Another example: can you imagine
typing (*((ARR)+(x+y))) every time you wanted ARR[x+y]?

But, the real question (as far as it has been discussed in lang.c, I
guess) is "if only pointers are used in ->, and structs in ., howcum
the compiler can't figure it out?"

Because, it is not PL/1.  I don't want an automatic conversion.  If I
type something like

   indexvalues[j]->fred[3] += 17

I want to be warned if I forgot that indexvalues is really an array of
structs, not array of pointer to struct.  Automatic adjustment could
lead to maintenance headaches later, or even debugging headaches
before it works the first time.

Just an opinion.  But then again, most of what get said here is. :-)
-- 
Randal L. Schwartz, Stonehenge Consulting Services (503)777-0095
on contract to BiiN Technical Publications (for now :-), Hillsboro, Oregon
<merlyn at intelob.intel.com> or ...!tektronix!ogcvax!omepd!intelob!merlyn
Standard disclaimer: I *am* my employer!



More information about the Comp.unix.questions mailing list