Adding two pointers

Karl Heuer karl at haddock.ima.isc.com
Tue May 9 09:05:41 AEST 1989


In article <4646 at freja.diku.dk> njk at freja.diku.dk (Niels J|rgen Kruse) writes:
>The word "array" as used when saying that two pointers must point into the
>same array in order to subtract them is a bit foggy to me.  What about
>	struct { char c; double d; } x[10];
>Are (& x[3].d) and (& x[7].d) not pointing into x?

Not in this sense.  They point into (different) arrays of type `double [1]'.

>Is (&x[3].d - &x[7].d) undefined?

Yes (even after I fixed the typo).  On some machines, there does not exist an
integer k such that (&x[1].d == &x[0].d + k), so it would be meaningless to
attempt to evaluate (&x[1].d - &x[0].d) to any integer.

>What array is (char *)x pointing into?

An array of type `char [sizeof(x)]' (or equivalently, `char [10*sizeof(S)]'
where S is the struct type) whose existence is supposedly implied by the pANS
statement that `objects are composed of bytes'.

Karl W. Z. Heuer (ima!haddock!karl or karl at haddock.isc.com), The Walking Lint



More information about the Comp.lang.c mailing list