C machine

News system news at ism780c.UUCP
Thu Jan 28 06:58:06 AEST 1988


In article <7159 at brl-smoke.ARPA> gwyn at brl.arpa (Doug Gwyn (VLD/VMB) <gwyn>) writes:
>In article <28700025 at ccvaxa> aglew at ccvaxa.UUCP writes:
>>I suppose that there is no equivalent to %p for pointers for
>>ptrdiff_t?
>
>Why should there be?  printf( "%ld", (long)( - p1) );

The problem with a header file defining a type ptrdiff_t is that the the
defined type must be one of the types in the base C language.  If an
implementation defines ptrdiff_t such that sizeof(ptrdiff_t) == sizeof(void*)
then p1-p2 may overflow.

BTW, the definition of pointer difference in K&R is "if p and q point to
members of the same array , p-q is the number of elements between p and q."

One way to interpret this is the following:

     el1  el2  el3
      ^         ^
      |         |
      p         q

As can be seen from the diagram the number of elements "between" p and q is
1 (not 2).  Furthermore, the number of elements between p and q is clearly
the same as the number of elements between q and p.  i.e, p-q == q-p and no
overflow is possible.  I hope the defininition in the proposed standard is
less ambigious than the wording in K&R.

       Marv Rubinstein -- Interactive Systems



More information about the Comp.lang.c mailing list