Pointers in comparison expressions

Geoffrey Keating geoffk@geoffk.org
Fri Jul 22 23:20:00 GMT 2005


Mirco Lorenzoni <mrc.lrn@inwind.it> writes:

> Can a pointer appear in a C/C++ relational expression which doesn't test the 
> equality (or the inequality) of that pointer with respect to another pointer?

Yes.

> For example, are the comparisons in the following program legal code?

No.

> /* test.c */
> #include <stdio.h>
> 
> int main(int argc, char* argv[])
> {
> 	void *a, *b;
> 	int aa, bb;
> 
> 	a = &aa;
> 	b = &bb;
> 	
> 	printf("a: %p, b: %p\n", a, b);
> 	if (a < b) 

Because 'a' and 'b' are not part of the same array, the behaviour is
undefined.

However, you can say

char a[100];

if (a + 3 >= a + 6)
  abort ();

which has defined behaviour (does not call abort()).



More information about the Gcc mailing list