home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!usc!rpi!psinntp!psinntp!dg-rtp!sheol!throopw
- From: throopw@sheol.UUCP (Wayne Throop)
- Newsgroups: comp.lang.c
- Subject: Re: pointer explanation
- Message-ID: <720937365@sheol.UUCP>
- Date: 05 Nov 92 01:29:33 GMT
- References: <895@bdrc.bdrc.bd.com>
- Lines: 61
-
- : From: jcl@bdrc.bd.com (John C. Lusth)
- : Message-ID: <895@bdrc.bdrc.bd.com>
- : " if((strcmp (*records[min].last, *records[j].last)) > 0) "
- : Given
- : struct entry **records;
- : then
- : *records[i].last
- : is wrong because * binds tighter than [],
-
- If the problem were a confusion between precedence of * and [], there
- wouldn't be a type mismatch, but a run-time subscripting error. We
- know that *x is equivalent to x[0], so the error you think you saw is
- just the difference between saying x[0][N] and x[N][0], both of which
- necessarily have the same type decomposition.
-
- : Would anyone care
- : to try a more succinct and clear headed explanation I can pass along
- : to my student?
-
- Actually [] binds tighter than *, so this properly indexes the list,
- landing on a pointer. The problem is that . ALSO binds closer than *,
- so the attempt is made to find a field of a non-struct (a pointer, that
- is). That's why you have to say
-
- (*foo).bar
- instead of
- *foo.bar
-
- The best idea is probably to rewrite the original "if" line as
-
- if((strcmp (records[min]->last, records[j]->last)) > 0)
-
- As an afterthought, the error message you quoted wasn't too
- informative. On a DECstation, gcc says
-
- request for member `f' in something not a structure or union
-
- lint says
-
- struct/union or struct/union pointer required
-
- the bundled K&R-plus-ANSI-extensions C compiler says
-
- ccom: struct/union or struct/union pointer required
- f(){ return *spp[23].f; }
- ---------------------^
- ccom: illegal indirection
- f(){ return *spp[23].f; }
- ----------------------^
-
- the unbundled ANSI C compiler says
-
- "spp[23]" has a pointer type, but occurs in a
- context that requires a union or struct.
-
- The clearest message is the one from the K&R-plus-ANSI-extensions
- compiler, because it makes it clear that the "." is being parsed
- before the "*". All of these make it clear that there is a type
- problem with the "." operation.
- --
- Wayne Throop ...!mcnc!dg-rtp!sheol!throopw
-