Adding two pointers

Niels J|rgen Kruse njk at freja.diku.dk
Sun May 7 02:24:14 AEST 1989


In article <17340 at mimsy.UUCP>, chris at mimsy.UUCP (Chris Torek) writes:
>
> But subtraction of pointers is a common occurrence in C, and any
> optimising compiler worth its name should note this.  Subtraction of
> two pointers is only defined if both point to elements of the same
> array; and when they do, it is guaranteed that the difference is a
> whole number of elements, or in other words a proper multiple of `size'.

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?

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

What array is (char *)x pointing into?

I seem to remember to have seen statements in this newsgroup by
people enjoying gurustatus, that dpANS C provides a way to get
offsets of fields within structs. What would be the use of that
if objects like x are not flat?
-- 
         Niels J|rgen Kruse
Email    njk at diku.dk
Mail     Tustrupvej 7, 2 tv, 2720 Vanlose, Denmark



More information about the Comp.lang.c mailing list