home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / lang / c / 12486 < prev    next >
Encoding:
Internet Message Format  |  1992-08-18  |  1.9 KB

  1. Path: sparky!uunet!olivea!decwrl!decwrl!world!ksr!jfw
  2. From: jfw@ksr.com (John F. Woods)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Array of pointers to structures
  5. Message-ID: <14893@ksr.com>
  6. Date: 18 Aug 92 16:45:13 GMT
  7. References: <Bt5Eq2.ExL@news.cso.uiuc.edu>
  8. Sender: news@ksr.com
  9. Lines: 35
  10.  
  11. jsanders@symcom.math.uiuc.edu (John Eric Sanders) writes:
  12. >struct strig 
  13. >{  char elment[80]; } ;
  14. >struct strig *ptr[100];
  15. >However, I can't seem to access a particular character, which I need to.
  16. >For example, I want to access the 5th character of the 75th line of
  17. >data, but *ptr[74].elment[4] = 'n' doesn't seem to work.  Should it?
  18.  
  19. Let's take this one bit at a time.
  20. ptr[74] is a pointer to a struct strig (namely, the 75th pointer in the
  21. array).  To use this pointer to access the "elment" element of the structure,
  22. you use the -> operator, as in 
  23.     ptr[74]->elment
  24. From that array, you want to select the 5th character, so you use
  25.     ptr[74]->elment[5]
  26. No indirection operator (unary *) is required here; in particular, due to
  27. operator precedence, your code was trying to treat ptr[74] as a structure,
  28. rather than a structure pointer (. binds more tightly than unary *).
  29.  
  30. >Do I have a bum compiler?
  31.  
  32. Could well be, but you haven't proven it yet :-).
  33.  
  34. >I was doing some tests and found that
  35. >ptr[5]->elment[2] is not equal to (*ptr[5]).elment[2], like the book
  36. >I have says it should be.
  37.  
  38. Now, we know what the first says (see above), what does the second say:
  39. take the 6th element of the ptr array, which is a pointer to struct strig,
  40. indirect through it to obtain a structure, select the elment element of that
  41. structure, and the 3rd character thereof.  Gee, that looks like it ought to
  42. be the same to me.  Oh dear.  Are you sure that you have initialized all of
  43. the pointers in the array ptr?  If not, then both of these will point into
  44. random memory, which could potentially change in between attempts to look
  45. into it in two different ways (or even just generate some form of exception).
  46.