home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / lang / c / 16013 < prev    next >
Encoding:
Internet Message Format  |  1992-11-04  |  2.3 KB

  1. Path: sparky!uunet!usc!rpi!psinntp!psinntp!dg-rtp!sheol!throopw
  2. From: throopw@sheol.UUCP (Wayne Throop)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: pointer explanation
  5. Message-ID: <720937365@sheol.UUCP>
  6. Date: 05 Nov 92 01:29:33 GMT
  7. References: <895@bdrc.bdrc.bd.com>
  8. Lines: 61
  9.  
  10. : From: jcl@bdrc.bd.com (John C. Lusth)
  11. : Message-ID: <895@bdrc.bdrc.bd.com>
  12. :         " if((strcmp (*records[min].last, *records[j].last)) > 0) "
  13. :     Given
  14. :         struct entry **records;
  15. :     then
  16. :         *records[i].last
  17. :     is wrong because * binds tighter than [],
  18.  
  19. If the problem were a confusion between precedence of * and [], there
  20. wouldn't be a type mismatch, but a run-time subscripting error.  We
  21. know that *x is equivalent to x[0], so the error you think you saw is
  22. just the difference between saying x[0][N] and x[N][0], both of which
  23. necessarily have the same type decomposition.
  24.  
  25. : Would anyone care
  26. : to try a more succinct and clear headed explanation I can pass along
  27. : to my student?
  28.  
  29. Actually [] binds tighter than *, so this properly indexes the list,
  30. landing on a pointer.  The problem is that . ALSO binds closer than *,
  31. so the attempt is made to find a field of a non-struct (a pointer, that
  32. is).  That's why you have to say
  33.  
  34.                   (*foo).bar 
  35. instead of
  36.                   *foo.bar
  37.  
  38. The best idea is probably to rewrite the original "if" line as
  39.  
  40.         if((strcmp (records[min]->last, records[j]->last)) > 0)
  41.  
  42. As an afterthought, the error message you quoted wasn't too
  43. informative.  On a DECstation, gcc says
  44.  
  45.     request for member `f' in something not a structure or union
  46.  
  47. lint says
  48.  
  49.     struct/union or struct/union pointer required
  50.  
  51. the bundled K&R-plus-ANSI-extensions C compiler says
  52.  
  53.     ccom: struct/union or struct/union pointer required
  54.           f(){ return *spp[23].f; }
  55.           ---------------------^
  56.     ccom: illegal indirection
  57.           f(){ return *spp[23].f; }
  58.           ----------------------^
  59.  
  60. the unbundled ANSI C compiler says
  61.  
  62.     "spp[23]" has a pointer type, but occurs in a
  63.                   context that requires a union or struct.
  64.  
  65. The clearest message is the one from the K&R-plus-ANSI-extensions
  66. compiler, because it makes it clear that the "." is being parsed
  67. before the "*".  All of these make it clear that there is a type
  68. problem with the "." operation.
  69. --
  70. Wayne Throop  ...!mcnc!dg-rtp!sheol!throopw
  71.