This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: Pointers in comparison expressions
- From: Geoffrey Keating <geoffk at geoffk dot org>
- To: mrc dot lrn at inwind dot it
- Cc: gcc at gcc dot gnu dot org
- Date: 13 Jul 2005 14:58:29 -0700
- Subject: Re: Pointers in comparison expressions
- References: <200507121825.45472.mrc.lrn@inwind.it>
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()).