4

Assigning variables with values during definition is a good practice.

A common practice is to assign variables with 0 and pointers with NULL.

 int p = NULL; // instead of int p = 0; int *ptr = NULL; int &ref=p; 

Are there reasons to assign NULL instead of 0 to a non-pointer variable type? The int p = NULL code compiles in Visual Studio, but it seems it may be less readable than assigning to zero.

2
  • 8
    C++ uses nullptr which is not a numerical type. Don't tag questions as C/C++ Commented Feb 23, 2016 at 16:49
  • 3
    on some machines a NULL is not 0, so assigning NULL to a non pointer variable generates trash. I.E. use the right types for all your assignments. Then 1) can be reasonably assured that the desired result was acheived. 2) will avoid the compiler having to perform an implicit conversion 3) will avoid getting your code rejected during a peer review. Commented Feb 25, 2016 at 0:00

5 Answers 5

21

The macro NULL is a null-pointer constant and has either an integer type or a pointer type.

Using NULL to assign or initialize a non-pointer variable will lead to question marks from other programmers at the least and it might result in compiler failures.
A line like

int a = NULL; 

is not considered good code and it will make the code less readable.

8
  • i had the same doubt, but since the code got compiled in visual studio I posted it here. Thanks for the answer Commented Feb 23, 2016 at 8:35
  • 5
    @evk1206 Not all that compiles is good... Commented Feb 23, 2016 at 10:52
  • 4
    @evk1206: It is compiler dependent if NULL has an integer or pointer type. That is why I said that it might cause a compilation failure. Commented Feb 23, 2016 at 11:44
  • 2
    Better: #define ZERO 0 and use int a = ZERO; :) (just kidding: while integer literals are generally code-smell and should be replaced with constants, 0 and 1 are usually an exception) Commented Feb 23, 2016 at 12:54
  • 2
    It may or may not compile, depending on the implementation. Whether it compiles or not, it is bad practice and makes me think the author is confused about whether p is a pointer or not, and if they are confused about that, what else are they confused about? Commented Feb 23, 2016 at 16:53
9

I think @R Sahu's answer reaches the right conclusion, but the supporting evidence it provides (based on a single implementation) is somewhat weak, at best.

This is tagged with both and . The details of how NULL is defined vary between the two, and also varies over time for C++. In all cases, NULL must expand to an "implementation defined null pointer constant". What varies is the definition of "null pointer constant".

In C, a null pointer constant must be an integer literal with the value 0, or the same cast to type "pointer to void"1. An integer with a non-zero value (by itself, or cast to type "pointer to void") is not allowed2. So, 0, 0L and ((void *)0) are all allowed, but something like ((void *)1234) is not.

In C++ 98/03, NULL must also expand to a null pointer constant--but with a somewhat different definition of the term--in particular, casting the integer literal to type "pointer to void" is not allowed in C++ 98/03. This means 0 and 0L are both allowed (and so would '\0' or, if you wanted to be really perverse, (3-(2 + 1)).

In C++ 11, the nullptr_t type and nullptr literal were added to C++, and nullptr is also allowed as a null pointer constant3. NULL is allowed to expand to any null pointer constant, so it could be 0, 0L (etc.) or nullptr, but (again) cannot be any non-zero integer, nor can it have type "pointer to void" (or "pointer to char", etc.)

The intent, however, has always been that NULL only be used to represent a null pointer. Although both C and C++ allow it to be defined as an unadorned 0 (for one example), so it might be possible to assign it to a variable of type int, there's no guarantee that code that does so will even compile (and a fair number of people believe that it shouldn't compile).

Conclusion: you should only ever assign NULL to a pointer. For new code, I'd advise using NULL only in C, and using nullptr in C++. For existing C++ code, I'd convert to using nullptr when any other refactoring is being done on that code, but would not usually modify the code solely to change NULL to nullptr (but code that assigns NULL to any non-pointer type should be corrected immediately, in either C or C++).


1. The wording in the C standard (§6.3.2.3/3) reads:

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

The italics (which are in the original) mean that this is considered the definition of that term for this standard.

2. Assigning a null pointer constant to a pointer variable may result in a non-zero value being stored into that variable. Although somewhat unusual, this is perfectly allowable. Even if/when that is the case, however, comparing that variable to a null pointer constant (e.g., NULL or 0) must yield true.

Likewise, an implementation is free to define any number of other integer constants that will produce a null pointer when assigned to a pointer variable. This doesn't affect the requirement on how NULL must be defined.

3. Here, the official wording (from [conv.ptr]) reads:

A null pointer constant is an integer literal (2.13.2) with value zero or a prvalue of type std::nullptr_t.

Here again, the italics indicate that this is the official definition of the term for that standard.

4
  • Yes, I found that quote for C too. But that only says that those constructs are called null pointer constants, not that those are the only null pointer constants. So, __null could be a null pointer constant too, as an example. Commented Feb 23, 2016 at 19:45
  • @Deduplicator: According to the ISO: "A definition is a single phrase that can replace the term wherever used." In other words, the definition means null pointer contsant is essentially a macro that expands to "An integer constant expression with the value 0, or such an expression cast to type void *". Commented Feb 23, 2016 at 20:05
  • I don't think that interpretation is quite water-proof, because in this cases it doesn't say "X is ...", but "... are called X". The latter isn't quite a definition of the term X, just a list of some things fulfilling it. That may be overly pedantic though... Compare with the nearby definitions of other terms, or the one for C++ you quoted, which are rather unambiguous in contrast. Commented Feb 23, 2016 at 21:45
  • 1
    @Deduplicator: the fact that it's in italics makes it unambiguous that this is a definition. Commented Feb 23, 2016 at 23:04
1

VS 2008 defines NULL as:

/* Define NULL pointer value */ #ifndef NULL #ifdef __cplusplus #define NULL 0 #else #define NULL ((void *)0) #endif #endif 

If you are writing a C program and use:

int i = NULL; 

that will expand to

int i = ((void *)0); 

If someone were to write that code manually, I would be very suspicious of their skills a software developer.

For that reason, I would strongly discourage use of

int i = NULL; 
0

The NULL pointer isn't required to be 0. A standards conforming implementation could look like this

#define NULL (void*)0x1234 

Secondly the NULL pointer has to compare equal 0 in a comparison. So the implementation would have to ensure hat if ((void*)0x1234) evaluates as false.

I'm not aware of any implementation doing this, but if you expect NULL to work interchangeably with 0 you're outside the C (or C++) specification.

1
  • 1
    But (void *) 0 always returns a NULL pointer even if the null pointer on a platform is not zero. I have worked on a platform where the NULL pointer was the most negative integer before. Commented Feb 24, 2016 at 10:57
-4

The short answer is no, you should never assign NULL to a non-pointer variable.

The use of NULL and nullptr in C and C++ are, IMOSVHO, extremely silly. NULL is defined as either 0 or some typecast of 0 like ((void *)0) which is really the same thing. To me, 0 means null. 0 is brief and easy to read and can be used anywhere you see NULL or nullptr in C and C++.

In languages like Javascript, the null type is something else and it can be quiet useful. In JSON it differentiates between no value (i.e. null) and zero which are two different things. But, since C and C++ don't really have a null type it's mostly pointless there.

Some people argue that NULL makes code more readable but it takes up more typing and clutters up the screen. I advocate for consistent code formatting but also compact code because it lets you see more at once which makes it easier to compare different parts of the code. Despite all this, you will frequently find NULL and now nullptr, where 0 would suffice, in otherwise well written C and C++ code.

So my advice is, don't assign ints to NULL and don't give in to peer pressure and assign pointers to NULL in C or C++ either. 0 == NULL.

comp.lang.c FAQ list · Question 5.9

Q: If NULL and 0 are equivalent as null pointer constants, which should I use?

A: Many programmers believe that NULL should be used in all pointer contexts, as a reminder that the value is to be thought of as a pointer. Others feel that the confusion surrounding NULL and 0 is only compounded by hiding 0 behind a macro, and prefer to use unadorned 0 instead. There is no one right answer. (See also questions 9.4 and 17.10.) C programmers must understand that NULL and 0 are interchangeable in pointer contexts, and that an uncast 0 is perfectly acceptable. Any usage of NULL (as opposed to 0) should be considered a gentle reminder that a pointer is involved; programmers should not depend on it (either for their own understanding or the compiler's) for distinguishing pointer 0's from integer 0's.

It is only in pointer contexts that NULL and 0 are equivalent. NULL should not be used when another kind of 0 is required, even though it might work, because doing so sends the wrong stylistic message. (Furthermore, ANSI allows the definition of NULL to be ((void *)0), which will not work at all in non-pointer contexts.) In particular, do not use NULL when the ASCII null character (NUL) is desired. Provide your own definition

#define NUL '\0'

if you must.

References: K&R1 Sec. 5.4 pp. 97-8 K&R2 Sec. 5.4 p. 102

4
  • stackoverflow.com/questions/2597142/… Commented Feb 15, 2018 at 3:49
  • 2
    Well, to you a 0 may mean int* but to the compiler it means int. There is a world of difference between the two (they are not "really the same thing"). My advice is, don't give in to bad advice. Commented Feb 15, 2018 at 5:57
  • But they are the same thing except on some really only machines. Commented Mar 8, 2018 at 23:08
  • And comp.lang.c FAQ agrees that it is reasonable to use 0 instead of NULL in all pointer contexts. See added quotation above. Commented Mar 8, 2018 at 23:16

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.