Two questions about pointers

Doug Gwyn gwyn at smoke.BRL.MIL
Fri Oct 20 23:49:29 AEST 1989


In article <1989Oct18.121516.10695 at anucsd.oz> bdm at anucsd.oz (Brendan McKay) writes:
>(1) Is it true that two pointers of the same type which point to the
>same object must compare equal?

A correct statement would be:

Two pointers that are constructed by following the rules in the Standard,
and for which the Standard says they point to the same object, are indeed
required to compare equal.

If you construct your pointers-to-the-same-object by sneaky means for
which the Standard makes no such guarantee, then they may compare
unequal.  Of course, in MOST implementations they would compare equal
in any case, but not in ALL implementations.

>(2) If two pointers of the same type compare equal, and each is cast
>to a second pointer type, must the results compare equal?

Again, if you have not stepped beyond the bound of what the Standard
guarantees, then the resulting pointers will also compare equal.

There are certain constraints that are required to be observed; for
example, alignments must be suitable.

>One can design weird pointer implementations which obey all the
>rules relating to pointers, as far as I can see, but which do not
>have one or both of these properties.

The implementations don't have to be especially weird.  Some
implementations are in fact expected to have to perform an
explict "normalizing" operation before testing for pointer equality.



More information about the Comp.std.c mailing list