Claudio Cortese
Claudio Cortese

Reputation: 1380

Is there a difference between initialiazing variables and pointers with NULL or 0?

Which of the following methods is a more correct way to initialize a variable?

int x = 0;
int x = NULL;

What about the pointers? I have been told discordant things about NULL, like : "NULL is best for initializing pointers" or "Don't use NULL but 0 to initialize a variable" and so on... Now, I read on the internet that NULL is equal to 0 and I tested that myself. So what's the point? Why are some people saying that using NULL isn't a good choice? Is there something that I'm missing?

Upvotes: 4

Views: 247

Answers (7)

Steve Summit
Steve Summit

Reputation: 47942

Definitely use NULL when you're initializing (or comparing) a pointer; it's good style.

Definitely don't use NULL when you're working with integers; it's poor style (and it might very well not work.)

Don't listen to anyone suggesting that you shouldn't use NULL with pointers; they're probably confused.

See the C FAQ list at http://c-faq.com/null/index.html for more information.

Upvotes: 1

chux
chux

Reputation: 153457

Which of the following methods is a more correct way to initialize a variable?

// int x = NULL;  `NULL` implies pointer
int x = 0;        // 0 implies integer value and x is an integer.

What about the pointers?

void *p = NULL;  // `NULL` implies pointer and p is a pointer.
//void *p = 0;   // 0 implies integer value

that NULL is equal to 0

It is equal in value, though maybe not in bit pattern. See below +0.0, -0.0 example.


If NULL was equal to 0, always, then there would not be a need for it in C.

NULL is a null pointer consonant - it often, but does not always have a bit pattern of zeros. A pointer with a NULL value will always compare equally to 0: same value, different bit patterns.

Remember that == compares values, not bit patterns.

void *a = NULL;
if (a == NULL) Always_True();
if (a == 0) Always_True();

Example that may help. +0.0 and -0.0 have the same value, same type, but different bit patterns. Similar thing may happen with pointers.

int main(void) {
  double pz = +0.0;
  double nz = -0.0;
  printf("=:%d  bits:%d\n", pz == nz, memcmp(&pz, &nz, sizeof pz) == 0);  // =:1  bits:0
  return 0;
}

Upvotes: 4

John Bode
John Bode

Reputation: 123458

Don't use NULL for initializing non-pointer types, as it may expand to something like (void *) 0 depending on the implementation. Use plain 0 (or a macro or const-qualified variable that evaluates to plain 0) for integral types.

Upvotes: 1

Sourav Ghosh
Sourav Ghosh

Reputation: 134326

NULL is a null-pointer constant. You should use that to initialize pointer types.

From C11, chapter §7.19, stddef.h

NULL
which expands to an implementation-defined null pointer constant.

and for null-pointer constant in C, chapter §6.3.2.3

An integer constant expression with the value 0, or such an expression cast to type void *, is called a null pointer constant.[...]

So, the type is that of a pointer.

For other non-pointer variables, you should be using 0.

Upvotes: 2

wallyk
wallyk

Reputation: 57774

There are/were some architectures deployed for which NULL is not zero: Prime 50, Honeywell-Bull, CDC Cyber 180 series and others. In these cases, NULL is most definitely not interchangeable with zero.

See this answer to another question for details of the machines affected.

Upvotes: -1

i486
i486

Reputation: 6563

Theoretically NULL can be pointer to address different from 0. But in practice such definition will crash large percent of software. And it is 0 (or (void *)0) in 99.99% of cases.

Upvotes: -1

dbush
dbush

Reputation: 223852

NULL is a pointer constant. You use this to initialize a pointer to a value that says it doesn't point to anything.

On most C implementations, it is defined as:

#define NULL ((void *)0)

But there's no guarantee of that.

Upvotes: 5

Related Questions