Adding two pointers
Bjorn Engsig
bengsig at oracle.nl
Thu May 11 22:07:12 AEST 1989
In article <1321 at infmx.UUCP> kevinf at infmx.UUCP (Kevin Franden) writes:
=
= struct{...}foo;
=
= foo *p1,*p2,*p3;
/* I added the *'s in the above declarations */
/* And I would add the following: */
int x,y;
=
= x=37;
= y=4;
=
= p1=a[x];
= p2=a[y];
= p3=(x+y)*sizeof(foo);
=
= I realize that I missed part of this discussion, sorry if this has been
= posted already, but won't p3 be pointing to the 41st element?
Hmm, yes, but the construct is highly debatable ...
= if this
=is true, this is pointer addition, no?
No, it's integer addition, and a highly debatable computation of a pointer,
and an assignment which needs a cast.
In pointer arithmetic, you can achieve what you are trying by
p3 = p2 + (p1 - a)
which means let p3 equeal the current p2 plus the number of elements between
p1 and the beginning of the array a. Note, however that this is a pointer
plus an integer, the latter being the difference between two pointers.
--
Bjorn Engsig, ORACLE Europe \ / "Hofstadter's Law: It always takes
Path: mcvax!orcenl!bengsig X longer than you expect, even if you
Domain: bengsig at oracle.nl / \ take into account Hofstadter's Law"
More information about the Comp.lang.c
mailing list