This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Pointers in comparison expressions


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()).


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]