Adding two pointers

Norman Diamond diamond at diamond.csl.sony.junet
Mon May 8 15:17:26 AEST 1989


In article <4646 at freja.diku.dk> njk at freja.diku.dk (Niels J|rgen Kruse) writes:

>What about
>
>struct {
>  char c;
>  double d;
>} x[10];
>
>Are (& x[3].d) and (& x[7].d) not pointing into x?

They point "into" x, yes, as far as the English language is concerned.
But they do not point to ELEMENTS OF x.  You have to read technicalese
more carefully than ordinary English.

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

I think you mean (& x[3].d - & x[7].d).  Again, when you read the
technicalese carefully enough, you find that the answer is yes
(maybe surprisingly).

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

Any pointer can be cast to a (char *) pointer and back to its original
type, but the (char *) version doesn't have to have a legal meaning.
If you dereference it, you might get a core dump or worse.

--
Norman Diamond, Sony Computer Science Lab (diamond%csl.sony.co.jp at relay.cs.net)
  The above opinions are my own.   |  Why are programmers criticized for
  If they're also your opinions,   |  re-inventing the wheel, when car
  you're infringing my copyright.  |  manufacturers are praised for it?



More information about the Comp.lang.c mailing list