This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: C code working using CC but not with gcc
- From: Dan Hipschman <dsh at linux dot ucla dot edu>
- To: gkarthi29 <karthikeyan dot gangatharan at lntinfotech dot com>
- Cc: gcc-help at gcc dot gnu dot org
- Date: Wed, 16 Apr 2008 22:11:44 -0700
- Subject: Re: C code working using CC but not with gcc
- References: <16738838.post@talk.nabble.com>
- Reply-to: dsh at linux dot ucla dot edu
On Wed, Apr 16, 2008 at 09:45:37PM -0700, gkarthi29 wrote:
>
> #include<stdio.h>
> main()
> {
> char *x;
> if (*x==NULL)
> {
> printf("hello");
> }
> }
The behaviour of the program is undefined. 'x' is not initialized,
therefore dereferencing it may or may not cause the program to crash
(memory fault). The output of both CC (whatever compiler that happens
to be) and gcc is acceptable. If you want well-defined semantics you
must fix your program.
(On top of 'x' not being initialized, comparing '*x', which has type
'char' to NULL, which is considered to be pointer value is
questionable.) I'm not sure exactly what you were expecting the program
to do, so I can't really give you good suggestions on how to fix it.
You should initialize 'x' if you plan on dereferencing it, though.
Hope that helps.