Path: utzoo!utgpu!water!watmath!clyde!rutgers!husc6!hao!oddjob!gargoyle!ihnp4!ihlpf!nevin1 From: nevin1@ihlpf.ATT.COM (00704a-Liber) Newsgroups: comp.lang.c Subject: Pointers and Arrays (was: C machine) Message-ID: <3546@ihlpf.ATT.COM> Date: 29 Jan 88 20:57:51 GMT References: <461@auvax.UUCP> <28700025@ccvaxa> <7159@brl-smoke.ARPA> <8728@ism780c.UUCP> Reply-To: nevin1@ihlpf.UUCP (00704a-Liber,N.J.) Organization: AT&T Bell Laboratories - Naperville, Illinois Lines: 55 In article <8728@ism780c.UUCP> marv@ism780.UUCP (Marvin Rubenstein) writes: >BTW, the definition of pointer difference in K&R is "if p and q point to >members of the same array , p-q is the number of elements between p and q." I opened up my K&R, skimmed all of chapter 5 (Pointers and Arrays), and could not find this anywhere! (Maybe I didn't look hard enough :-)) I don't think this is how K&R defined it (see more comments below). > >One way to interpret this is the following: > > el1 el2 el3 > ^ ^ > | | > p q > >As can be seen from the diagram the number of elements "between" p and q is >1 (not 2). Furthermore, the number of elements between p and q is clearly >the same as the number of elements between q and p. i.e, p-q == q-p and no >overflow is possible. I hope the defininition in the proposed standard is >less ambigious than the wording in K&R. Suppose q pointed to el2 instead of el3. Would q-p === 0? And if this were true (which I maintain isn't), then what would q-p be if p and q pointed to the same element? Also, p-q is not equal to q-p (except in the trivial case where q === p). There is no implied absolute value taken when subtracting pointers. (p-q) === -(q-p) For justification of what I am saying, I submit the following program: ============================================================================== main() { int array[9]; int *p1; int *p2; p1 = &array[3]; p2 = &array[7]; printf("%d\t", (int)(p1 - p2)); printf("%d\n", (int)(p2 - p1)); } ============================================================================== When compiled and executed, this program produces: -4 4 QED I don't know where your ideas came from, but they certainly weren't from K&R. Maybe you were reading the new draft standard for ANSI C :-) :-)!! -- _ __ NEVIN J. LIBER ..!ihnp4!ihlpf!nevin1 (312) 510-6194 ' ) ) "The secret compartment of my ring I fill / / _ , __o ____ with an Underdog super-energy pill." / (_