102

I have a problem with this struct contructor when I try to compile this code:

typedef struct Node
{
    Node( int data ) //
    {
        this->data = data;
        previous = NULL; // Compiler indicates here
        next = NULL;
    }

    int data;
    Node* previous;
    Node* next;
} NODE;

when I come this error occurs:

\linkedlist\linkedlist.h||In constructor `Node::Node(int)':|
\linkedlist\linkedlist.h|9|error: `NULL' was not declared in this scope|
    ||=== Build finished: 1 errors, 0 warnings ===|

Last problem was the struct, but it worked fine when it was in my main.cpp, this time it's in a header file and is giving me this problem. I am using Code::Blocks to compile this code

unwind
  • 391,730
  • 64
  • 469
  • 606

5 Answers5

159

NULL is not a built-in constant in the C or C++ languages. In fact, in C++ it's more or less obsolete, just use a plain literal 0 instead, the compiler will do the right thing depending on the context.

In newer C++ (C++11 and higher), use nullptr (as pointed out in a comment, thanks).

Otherwise, add

#include <stddef.h>

to get the NULL definition.

GManNickG
  • 494,350
  • 52
  • 494
  • 543
unwind
  • 391,730
  • 64
  • 469
  • 606
  • 7
    NULL is part of stddef.h, not stdlib.h. Technically, you aren't guaranteed to get it as part of stdlib.h although I admit it would be pretty suprising if you didn't. – CB Bailey May 29 '09 at 06:39
  • 9
    NULL is defined in the following C headers: stddef.h, stdlib.h, stdio.h, locale.h, string.h, and time.h (and wchar.h if you count C99). – Michael Burr May 29 '09 at 07:04
  • OK, I stand corrected. I wonder why C++ needs to mention the contents of cstddef explicitly, and not the other headers. Perhaps it's considered the canonical 'C++' place for NULL whereas the other locations are for C compatibility? – CB Bailey May 29 '09 at 07:47
  • 2
    @Charles Bailey C++ mentions cstddef explicitly because it defines NULL to 0 and not to ((void *)0) as in C99. This is because in C++ implicit casts from void * are forbidden. – lothar May 29 '09 at 08:09
  • 2
    Oh and in the upcoming C++0x there will be a new keyword null_ptr. See http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n2105.html – lothar May 29 '09 at 08:11
  • 2
    I know why the standard needs to talk about the type of NULL, I just wonder why it only mentions one the places that its defined in the C standard library. At the moment, the new keywordin C++0x is still spelled 'nullptr'. – CB Bailey May 29 '09 at 09:26
  • 1
    NULL is defined in quite a lot of C headers - and is one of those headers (C99 for sure; I don't think it changed between C89 and C99). That said, is the smallest header to use if you aren't using anything else from . Also, given that this is C++, should that be ? – Jonathan Leffler May 29 '09 at 13:25
  • >> Perhaps it's considered the canonical 'C++' place for NULL << I think this is correct. In the C standard, wherever it's mentioned that NULL is defined in a header other than stddef.h, there's always a note to see the description of NULL for stddef.h. – Michael Burr May 29 '09 at 18:22
  • 2
    NULL, although a throw back to C is possibly better than '0', if for not other reason that when C++ 0x arrives with null_ptr, it will be easier to do a search & replace for NULL! – Richard Corden Jun 01 '09 at 10:01
  • 8
    `` is the cleaner option. – Fred Foo Mar 12 '12 at 10:59
  • 40
    DO NOT use "0" when you mean "NULL"! There is a _difference_: semantics. Never underestimate the importance of knowing what something is and using the right word, even if the compiler will let you get away with it! – geometrian Jul 07 '13 at 00:30
  • 2
    @IanMallett: If you're talking about the fact that the null pointer is not guaranteed to be composed of binary zeroes you're mistaken. `0` in a pointer context is the null pointer, even if this may mean it's not a binary zero. You can even use the ASCII NUL char (one L) `'\0'` for the null pointer. Actually to add insult to injury you can use any integral **constant expression** of value zero (not just a literal) for the null pointer, including `3-2-1` or `!!!!!1`. Apparently Stroustrup thought all this was a funny thing to do (see TCPPPL paragraph on that). – 6502 Aug 27 '13 at 09:13
  • 14
    @6502 Not talking about that; 0 and NULL do have the same value (almost) always, so using '\0' or 0 will accidentally work. The problem is semantics. Using NULL is more expressive, since it says that the value is question is a pointer, not just an integer. – geometrian Aug 30 '13 at 01:21
  • 2
    It's incorrect that in C++ 0 and '\0' will work only accidentally. It will always work, according to C++ standard. – gena2x Aug 01 '14 at 17:48
42

Do use NULL. It is just #defined as 0 anyway and it is very useful to semantically distinguish it from the integer 0.

There are problems with using 0 (and hence NULL). For example:

void f(int);
void f(void*);

f(0); // Ambiguous. Calls f(int).

The next version of C++ (C++0x) includes nullptr to fix this.

f(nullptr); // Calls f(void*).
Eric
  • 95,302
  • 53
  • 242
  • 374
  • 6
    It's defined as `((void *)0)` by most C standard library implementations. – Triang3l Sep 16 '13 at 10:55
  • 3
    This is the best **short** answer (and technically precise) I've ever read regarding the topic: NULL vs. 0 vs. nullptr. Thank you! – jose.angel.jimenez Sep 24 '14 at 11:14
  • 3
    @SiPlus `((void *)0)` is incorrect in C++, because `void*` is not coercible to other pointer types the way it is in C. glibc, for instance, does `#define NULL 0` when `__cplusplus` is defined. – rpjohnst Jul 08 '15 at 18:08
17

Are you including "stdlib.h" or "cstdlib" in this file? NULL is defined in stdlib.h/cstdlib

#include <stdlib.h>

or

#include <cstdlib>  // This is preferrable for c++
Andy White
  • 86,444
  • 48
  • 176
  • 211
  • 2
    NULL is part of stddef.h, not stdlib.h – awiebe Jan 18 '16 at 02:20
  • @awiebe That was what I thought until five minutes ago - according to C99 quite a few different header files have to define it. Section 7.17 requires stddef.h to, 7.20 requires it in stdlib.h, and there are probably a good few others. – AJM Mar 18 '20 at 10:38
16

NULL isn't a native part of the core C++ language, but it is part of the standard library. You need to include one of the standard header files that include its definition. #include <cstddef> or #include <stddef.h> should be sufficient.

The definition of NULL is guaranteed to be available if you include cstddef or stddef.h. It's not guaranteed, but you are very likely to get its definition included if you include many of the other standard headers instead.

CB Bailey
  • 755,051
  • 104
  • 632
  • 656
4

Don't use NULL, C++ allows you to use the unadorned 0 instead:

previous = 0;
next = 0;

And, as at C++11, you generally shouldn't be using either NULL or 0 since it provides you with nullptr of type std::nullptr_t, which is better suited to the task.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • 33
    I tend think that NULL is useful documentation that you are intending to use a null pointer constant rather than an integer constant, although I don't object to using 0. I'll admit that you don't gain any practical benefits at the moment, but if/when you adopt the next C++ version it gives a good start for places to change to use the new nullptr constant. – CB Bailey May 29 '09 at 06:53
  • 1
    i agree with you both, of course. Incidentally, it's both good that one documents one uses a pointer, but also good one documents that one actually puts an integer forward. consider printf("%p\n", NULL); // OH, UB. Or if you have two overloads, void f(int); void f(void*); you might think that f(NULL); calls the void* version when having a quick look on the call. f(0); will document the fact that it will actually call the int version, but won't document the fact that you intend you pass a pointer :( Good that nullptr fixes it :) – Johannes Schaub - litb May 29 '09 at 22:55