Serve La said:
C programmers get accused of writing platform dependant code when they write
int *x = 0;
They have to write
int *x = NULL;
because some platforms don't represent NULL as all bits zero.
C++ programmers don't seem to have this problem, they can write 0 whenever
they want. Why is that?
Actually, NULL is supposed to be 0. Conforming C compilers
recognize the value 0 (when used as a pointer) to be a
special value that means NULL. Even when the implementation
internally uses some other binary value to mean a NULL
pointer, the value 0 is what is used the source code. This
includes assignment, comparison, dereferencing, and casting
between integer and pointer types. The compiler translates
from 0 in the source code into its internal representation
of a NULL pointer.
This is why you can write:
if(x)
{
// x is non-NULL
}
which is exactly the same as:
if(NULL != x)
{
// x is non-NULL
}
which is also exactly the same as:
if(0 != x)
{
// x is non-NULL
}
The C compiler knows that 0 is being used in a pointer-type
expression and does the right thing.
--
----------------------------------------------
Jeffrey D. Smith
Farsight Systems Corporation
24 BURLINGTON DRIVE
LONGMONT, CO 80501-6906
303-774-9381
http://www.farsight-systems.com
z/Debug debugs your Systems/C programs running on IBM z/OS!