Hi,

On Mon, 1 Nov 2010, Alexander Graf wrote:

> > No. Pointers of same type which are not void pointers can be compared.
> > 
> > There is even a data type ptrdiff_t, so you can also compare their
> > difference with zero.
> 
> Let's ask someone who definitely knows :).
> 
> Michael, is code like
> 
> char *x = a, *y = b;
> if (x < y) {
>   ...
> }

Pointers can be compared iff they point into the same object 
(including right after the object), so it depends on what a and b were 
above.  This would be invalid for instance:

  int o1, o2;
  int *p1 = &o1, *p2 = &o2;
  if (p1 < p2) ...

> valid? Or do I first have to cast x and y to unsigned longs or 
> uintptr_t?

For doing a valid pointer comparison you don't have to cast anything.  
Casting doesn't make an invalid comparison valid.


Ciao,
Michael.

Reply via email to