This is the mail archive of the gcc-help@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: C code working using CC but not with gcc


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.


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