Re: C code working using CC but not with gcc

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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 of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux