When is a cast not a cast?

Richard Tobin richard at aiai.ed.ac.uk
Wed May 3 21:57:45 AEST 1989


>Addition of pointers is a meaningless operation.

Not always.  (I'm not discussing whether it's legal in C, just whether it's
meaningless.)

This is perfectly reasonable:

  char *p, *q, *r, *s;

   ...

  for(...)
  {
       ...
       s = p + (q - r);
       ...
  }

(assuming they point into the same array).

Now suppose p and q are constant in the loop, but r isn't.  Then you
might want to do:

  t = p + q;

  for(...)
  {
       ...
       s = t - r;
       ...
  }

Of course, there's no reasonable declaration for t, so you'd probably
have to use some casts and treat them as integers (no flames please;
for some people writing some programs sometimes portability to some
machines isn't important).

A good compiler might do this for you.

-- Richard
-- 
Richard Tobin,                         JANET: R.Tobin at uk.ac.ed             
AI Applications Institute,             ARPA:  R.Tobin%uk.ac.ed at nss.cs.ucl.ac.uk
Edinburgh University.                  UUCP:  ...!ukc!ed.ac.uk!R.Tobin



More information about the Comp.lang.c mailing list