Path: utzoo!utgpu!news-server.csri.toronto.edu!rutgers!mcnc!rti!sheol!throopw From: throopw@sheol.UUCP (Wayne Throop) Newsgroups: comp.lang.c Subject: Re: p[1] vs. *(p+1) Summary: Objection: the answer is misleading the witness Message-ID: <0901@sheol.UUCP> Date: 2 Sep 90 03:27:22 GMT References: <1990Aug31.190103.15043@laguna.ccsf.caltech.edu> Lines: 44 > From: bruce@seismo.gps.caltech.edu (Bruce Worden) >> (Paul Schmidt) >> *(p+1) does not add 1 to p, but instead adds >> sizeof(array_element_type) to p? > Yes. No. Or less bluntly, that's not (IMHO) a very good way of putting it. Bruce is right as far as he goes, but I think Paul's phrase above is misleading. What I'm objecting to is the dimensionless "adding one to it (a pointer)". One what? People most probably mean "one byte" by this, but I don't think peoples' understanding of pointer arithmetic is strong enough to trust their notions of "add one to" a pointer should be trusted. And I think history bears me out. Or to be specific, I druther see the above exchange be something like Q. *(p+1) does not add 1 to p, but instead adds sizeof(array_element_type) to p? A. No. It produces a pointer value that references the next array_element_type array element after the one referenced by p. On byte addressed machines, the bit pattern of this pointer, when treated as an integer, is often an integer sizeof(array_element_type) greater than the integer gotten by treating the bits of p as an integer. But this latter fact is a coincidence produced by a particular implementation of C pointers. Now, I suppose this seems awfully picky. "Didn't he just say that" and all that. But the point is that the underlying assumption of a smooth byte-granular address space with pointers represented as integer offsets into this address space is NOT the best way of explaining pointers, even if it is the one so many people use. Or to try putting it another way, I think it is misleading to say that "adding one to p 'really' adds sizeof(*p) to p". I disagree. I say it "really" adds 1, and the result points at an element in the array into which p points which is offset exactly 1 array element from where p points. -- Wayne Throop !mcnc!rti!sheol!throopw or sheol!throopw@rti.rti.org