home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: sparky!uunet!usc!sdd.hp.com!saimiri.primate.wisc.edu!ames!daffodil!wyvern!waggen!alpha
- From: alpha@waggen.twuug.com (Joe Wright)
- Subject: Re: pointer explanation
- Message-ID: <1992Nov5.041803.7910@waggen.twuug.com>
- Organization: ALPHA, Box 68621, Va. Beach, VA 23455
- X-Newsreader: TIN [version 1.1 PL6]
- References: <895@bdrc.bdrc.bd.com>
- Date: Thu, 5 Nov 1992 04:18:03 GMT
- Lines: 100
-
- John C. Lusth (jcl@bdrc.bd.com) wrote:
- : I am teaching an introductory course in C. One of my students,
- : working on a programming assignment, posed me a question.
- :
- : Why would the following lines of code generate
- : an error message that states "value has no
- : members"
- :
- : " if((strcmp (*records[min].last, *records[j].last)) > 0) "
- :
- : OR
- :
- : " if((strcmp (records[min].last, records[j].last)) > 0) "
- :
- : where records is a struct entry **.
- :
- :
- :
- : The layout of the data structure the student is dealing with is...
- :
- : ___________________________________
- : records---->|_____|_____|_____|_____|_____|_____|
- : | |
- : | |
- : ------- | |
- : | first | | |
- : |-------| | |
- : | last |<-- |
- : |-------| |
- : | age | |
- : ------- |
- : ------- |
- : | first | |
- : |-------| |
- : | last |<--
- : |-------|
- : | age |
- : -------
- :
- : The answer I gave was...
- :
- : Given
- :
- : struct entry **records;
- :
- : then
- :
- : *records[i].last
- :
- : is wrong because * binds tighter than [], so the above is really
- :
- : (*records)[i].last
- :
- : which devolves to
- :
- : *((*records) + i).last
- :
- : This is wrong because records holds the address of the first
- : element of the array, which is the address of a structure. You
- : are saying, get the first address in the array and add i to it.
- : Well, the first address does not point to an array so it makes no
- : sense to add an offset. It is the address of a structure. What you
- : really want is
- :
- : *(records[i]).last
- :
- : which devolves to
- :
- : *(*(records + i)).last
- :
- : Now you are getting the address in the ith element of the array and
- : then following the pointer.
- :
- : The code
- :
- : records[i].last
- :
- : is clearly wrong because records[i] is of type pointer to struct
- : entry, not struct entry, so it has no members.
- :
- :
- : It seems to me that I didn't do a very good job in explaining the
- : difference, at least for the first code fragment. Would anyone care
- : to try a more succinct and clear headed explanation I can pass along
- : to my student?
- :
- : john
- : --
- : John C. Lusth, Becton Dickinson Research Center, RTP, NC, USA jcl@bdrc.bd.com
-
- It's all 'wrong'. Your graphic indicates an array of structs with records
- pointing to the array. If so, the declaration is 'struct entry *records;'
- and 'records[i].last' is right. If 'struct entry **records;' is right
- then your graphic is all wet. In this case, records[i] is a pointer,
- not a structure. Use 'records[i]->last' or (*records[i]).last
-
- Finally, * does not bind tighter than . [] -> or (). These are the tightest.
-
- --
- Joe Wright alpha@waggen.twuug.com
-