This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Pointers in comparison expressions
- From: Mirco Lorenzoni <mrc dot lrn at inwind dot it>
- To: gcc at gcc dot gnu dot org
- Date: Tue, 12 Jul 2005 18:25:45 +0200
- Subject: Pointers in comparison expressions
- Reply-to: mrc dot lrn at inwind dot it
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?
For example, are the comparisons in the following program legal code?
/* 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)
printf("a < b\n");
else
printf("a >= b\n");
if (b < a)
printf("b < a\n");
else
printf("b >= a\n");
return 0;
}
The execution of the compiled program produces an output like this:
a: 0xbffff55c, b: 0xbffff558
a >= b
b < a
I have compiled this program with gcc and g++, versions 3.4.4 and 4.0.1, and
(the prehistoric) egcs 2.91.66. None of these compilers has issued any
warning or error. I have compiled the program above whit these options: -Wall
--ansi --pedantic .
These are the version information about the compilers:
Reading specs from /usr/local/lib/gcc/i686-pc-linux-gnu/3.4.4/specs
Configured with: ../gcc-3.4.4/configure --srcdir=../gcc-3.4.4
--mandir=/usr/local/share/man --infodir=/usr/local/share/info
--enable-threads --enable-languages=c,c++,f77,java,objc --prefix=/usr/local
--with-cpu=pentium2 --with-tune=k8 --enable-__cxa_atexit --with-x
--enable-java-awt=gtk
Thread model: posix
gcc version 3.4.4
Using built-in specs.
Target: i686-pc-linux-gnu
Configured with: ../gcc-4.0.1/configure --srcdir=../gcc-4.0.1
--mandir=/usr/local/share/man --infodir=/usr/local/share/info
--enable-threads --enable-languages=c,c++,f95,java,objc --prefix=/usr/local
--with-cpu=pentium2 --with-tune=k8 --enable-__cxa_atexit --with-x
--enable-java-awt=gtk
Thread model: posix
gcc version 4.0.1
Reading specs from /usr/lib/gcc-lib/i386-redhat-linux/egcs-2.91.66/specs
gcc version egcs-2.91.66 19990314/Linux (egcs-1.1.2 release)
I think that even if the use of relational operators other than '==' and '!='
is legal with pointers, the compiler should issue a warning (when the option
-Wall is used), as it does for assignment, used as truth values, not
surrounded with parentheses.
Thanks in advance for your reply.
Regards,
Mirco Lorenzoni
P.S.
I'm not a list subscriber. Send me a copy of your reply, please.