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