home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / lang / c / 16016 < prev    next >
Encoding:
Text File  |  1992-11-05  |  3.2 KB  |  112 lines

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!usc!sdd.hp.com!saimiri.primate.wisc.edu!ames!daffodil!wyvern!waggen!alpha
  3. From: alpha@waggen.twuug.com (Joe Wright)
  4. Subject: Re: pointer explanation
  5. Message-ID: <1992Nov5.041803.7910@waggen.twuug.com>
  6. Organization: ALPHA, Box 68621, Va. Beach, VA 23455
  7. X-Newsreader: TIN [version 1.1 PL6]
  8. References: <895@bdrc.bdrc.bd.com>
  9. Date: Thu, 5 Nov 1992 04:18:03 GMT
  10. Lines: 100
  11.  
  12. John C. Lusth (jcl@bdrc.bd.com) wrote:
  13. : I am teaching an introductory course in C.  One of my students,
  14. : working on a programming assignment, posed me a question.
  15. :     Why would the following lines of code generate
  16. :     an error message that states "value has no
  17. :     members"
  18. :     " if((strcmp (*records[min].last, *records[j].last)) > 0) "
  19. :     OR
  20. :     " if((strcmp (records[min].last, records[j].last)) > 0) "
  21. :     where records is a struct entry **.
  22. : The layout of the data structure the student is dealing with is...
  23. :          ___________________________________
  24. :     records---->|_____|_____|_____|_____|_____|_____|
  25. :            |     |
  26. :            |     |
  27. :         -------    |     |
  28. :        | first |   |     |
  29. :        |-------|   |     |
  30. :        | last  |<--      |
  31. :        |-------|         |
  32. :        | age   |         |
  33. :         -------          |
  34. :               -------    | 
  35. :              | first |   |
  36. :              |-------|   |
  37. :              | last  |<--
  38. :              |-------|
  39. :              | age   |
  40. :               -------
  41. : The answer I gave was...
  42. :     Given
  43. :     struct entry **records;
  44. :     then
  45. :     *records[i].last
  46. :     is wrong because * binds tighter than [], so the above is really
  47. :     (*records)[i].last
  48. :     which devolves to
  49. :     *((*records) + i).last
  50. :     This is wrong because records holds the address of the first
  51. :     element of the array, which is the address of a structure.   You
  52. :     are saying, get the first address in the array and add i to it.
  53. :     Well, the first address does not point to an array so it makes no
  54. :     sense to add an offset.  It is the address of a structure. What you
  55. :     really want is
  56. :     *(records[i]).last
  57. :     which devolves to
  58. :     *(*(records + i)).last
  59. :     Now you are getting the address in the ith element of the array and
  60. :     then following the pointer.
  61. :     The code
  62. :     records[i].last
  63. :     is clearly wrong because records[i] is of type pointer to struct
  64. :     entry, not struct entry, so it has no members.
  65. : It seems to me that I didn't do a very good job in explaining the
  66. : difference, at least for the first code fragment.  Would anyone care
  67. : to try a more succinct and clear headed explanation I can pass along
  68. : to my student?
  69. : john
  70. : -- 
  71. : John C. Lusth, Becton Dickinson Research Center, RTP, NC, USA  jcl@bdrc.bd.com
  72.  
  73. It's all 'wrong'.  Your graphic indicates an array of structs with records
  74. pointing to the array.  If so, the declaration is 'struct entry *records;'
  75. and 'records[i].last' is right.  If 'struct entry **records;' is right
  76. then your graphic is all wet.  In this case, records[i] is a pointer,
  77. not a structure.  Use 'records[i]->last' or (*records[i]).last
  78.  
  79. Finally, * does not bind tighter than . [] -> or ().  These are the tightest.
  80.  
  81. -- 
  82. Joe Wright  alpha@waggen.twuug.com  
  83.