NULL in C

S

Serve La

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?
 
J

Jirka Klaue

Serve 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.

0 is a null pointer constant and is guaranteed to yield a null pointer
when used in a pointer context, regardless of the actual bit pattern.

Jirka
 
X

xarax

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!
 
M

Mark Gordon

Actually, NULL is supposed to be 0.

Actually it is not required to be 0. (void*)0 is also a valid definition
of NULL.
Conforming C compilers
recognize the value 0 (when used as a pointer) to be a
special value that means NULL.

Only a constant expression evaluation to 0. I don't think an integer
containing a value 0 is guaranteed by the standard because it would be a
pain to implement if the null pointer was not all bits zero.
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.

<snip>

the rest looked about right.
 
M

Mathew Hendry

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.

The accusers are misinformed. This is a common source of confusion. NULL and
0 are interchangable with very few exceptions.
C++ programmers don't seem to have this problem, they can write 0 whenever
they want. Why is that?

The main difference between C and C++ in this area is that in C++, NULL must
be defined as

#define NULL 0

or equivalent. The alternative permitted in C

#define NULL (void *)0

is not acceptable in C++ because C++ does not allow implicit conversions
from void * to other pointer types. The C++ people viewed these implicit
conversions as a hole in the type system, and closed it.

-- Mat.
 
K

Keith Thompson

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?

The C FAQ is at <http://www.eskimo.com/~scs/C-faq/top.html>.
Section 5 is about null pointers.
 
S

Scott Fluhrer

Mathew Hendry said:
The accusers are misinformed. This is a common source of confusion. NULL and
0 are interchangable with very few exceptions.
int i = 0; /* Groovy */
int j = NULL; /* An error on some compilers */

:)
 
M

Martin Ambuhl

Serve said:
C programmers get accused of writing platform dependant code when they write
int *x = 0;

If they are so accused, it is by idiots.
They have to write
int *x = NULL;

If they are so required, it is by idiots.
because some platforms don't represent NULL as all bits zero.

This is not a reason and is completely irrelevant in pointer contexts.
C++ programmers don't seem to have this problem,

Neither do C programmers.
they can write 0 whenever they want.

As can C programmers.
> Why is that?

I don't know why your collection of at best half-truths exists. Your post
is one of the worst attempts at a troll I've ever seen.
 
K

keanu

I don't know why your collection of at best half-truths exists. Your post
is one of the worst attempts at a troll I've ever seen.

Yours on the other hand is pritty good.
 
I

Irrwahn Grausewitz

Scott Fluhrer said:
int i = 0; /* Groovy */
int j = NULL; /* An error on some compilers */

:)

Er, seems you're just joking, but anyway: the discussion was about
0 vs. NULL in pointer contexts.

int *i = 0; /* valid */
int *j = NULL; /* more idiomatic variant */
printf("%p",0); /* undefined behaviour */

Regards
 
M

Micah Cowan

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?

Neither do C programmers. The fact that NULL is not represented
as all bits zero is:

1) Not limited to C. This is also true of C++.
2) Not relevant to the initializations you have above.

Where it becomes a problem is more in code such as:

void **foo = calloc(NUM_ELEMS * sizeof *foo);
/* ... Code that assumes that foo[0] through foo[NUM_ELEMS-1]
are NULL pointers due to the all-bits-zero initialization from
calloc()... */

HTH,
Micah
 
P

pete

Serve La wrote:
C programmers get accused of writing
platform dependant code when they write
int *x = 0;

The accusers are ignorant.
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?

Your question depends on an invalid hypothesis.
 
S

Serve La

Martin Ambuhl said:
I don't know why your collection of at best half-truths exists. Your post
is one of the worst attempts at a troll I've ever seen.

Troll!?!? More misinformed than trolling and it was somebody here who said
it BTW.

Here's the worst troll. "Pascal is better than C"
 
J

Joe Wright

Irrwahn said:
Er, seems you're just joking, but anyway: the discussion was about
0 vs. NULL in pointer contexts.

int *i = 0; /* valid */
int *j = NULL; /* more idiomatic variant */
printf("%p",0); /* undefined behaviour */
The variadic printf() requires (void*)0 because the compiler doesn't
parse the format string and doesn't know the context of 0. If the
implementation has defined 'NULL 0' then (void*)NULL will be required.
 
I

Irrwahn Grausewitz

Serve La said:
Troll!?!? More misinformed than trolling and it was somebody here who said
it BTW.

If somebody here said so, he has most certainly been corrected.
Or maybe you just misunderstood something.

Regards
 
I

Irrwahn Grausewitz

Joe Wright said:
Irrwahn Grausewitz wrote:
The variadic printf() requires (void*)0 because the compiler doesn't
parse the format string and doesn't know the context of 0. If the
implementation has defined 'NULL 0' then (void*)NULL will be required.

That's the point.
 
C

Christian Bau

"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?

That's not a problem of the C language, that is a problem with people
who don't understand it.

Come back when you understand the difference between an integer zero, a
value with a representation of all bits zero, a null pointer, a null
pointer constant and the NULL macro.
 
K

Keith Thompson

Micah Cowan said:
Neither do C programmers. The fact that NULL is not represented
as all bits zero is:

1) Not limited to C. This is also true of C++.
2) Not relevant to the initializations you have above.

To be precise, NULL is not *necessarily* represented as all bits zero.
On many implementations, NULL is represented as all bits zero (which
can make it more difficult to detect bugs caused by code that makes
this assumption).
 
D

Dan Pop

In said:
Neither do C programmers. The fact that NULL is not represented
as all bits zero is:

NULL is a macro. Its usual representations are 0 or ((void *)0).
If you mean null pointer, NULL is *not* a valid abbreviation.

Dan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top