Does NULL need a header or did I mess something up?

Started by
21 comments, last by Qw3r7yU10p! 19 years, 6 months ago
Ok, I looked around and I can't find information anywhere saying that NULL requires a header, but I keep getting this error (using C++ and MSVC++ 6.0):
error C2065: 'NULL' : undeclared identifier
I remember cases where I had headers that used NULL, but I don't remember including any other header files. The error message goes away when I include <windows.h>, but I thought that the compiler was supposed to declare NULL. Any help would be greatly appreciated! Thanks!
Advertisement
Quote:Original post by Programmer16
Ok, I looked around and I can't find information anywhere saying that NULL requires a header, but I keep getting this error (using C++ and MSVC++ 6.0):
error C2065: 'NULL' : undeclared identifier


I remember cases where I had headers that used NULL, but I don't remember including any other header files.

The error message goes away when I include <windows.h>, but I thought that the compiler was supposed to declare NULL.

Any help would be greatly appreciated!
Thanks!


The compiler doesn't declare anything on its own just for the heck of it, so u need to include windows.h or possibly stdio.h, stdlib.h. One of those will have it aswell.
NULL is a c/c++ macro, so a few headers do define it. however, you can define it yourself as either :
#define NULL 0// or#define NULL ((void*)0)

the latter definition coming from here
- stormrunner
Ok, thanks.

That really sucks, because I thought I had messed something up. So, I spent all of yesterday backing up stuff and recovering my computer (which for some reason is running alot slower now) just to find out that it was fine.

Yeah, iostream, windows, stdio, etc seem to have it defined.

Oh, and since the topic is up, if NULL is just a macro, how come it is better to use it instead of 0?

Thanks again!

Edit:
I tried the void pointer macro, and it tells me that it can't convert from void* to Class*.
the latter definition is C-style, it doesn't work with C++, you have to
#define NULL 0L
Quote:Original post by Programmer16
Oh, and since the topic is up, if NULL is just a macro, how come it is better to use it instead of 0?


It's a matter of preference. Some people prefer 0, often citing the reason that some of the standard headers need to be included for it to be defined. Others prefer NULL, saying that it makes the code easier to read because you can instantly see that it is a pointer and not an integer. Personally, I prefer NULL, but it's a matter of opinion, really. It has been discussed here many times, and asking that question is like inviting both sides of the debate to a long flamewar ;-)
Given that Microsoft apparently likes hungarian notation, I'm still surprised that they don't have pvNULL or something.

^^^ sarcasm ^^^
I think the reason usually given for not using NULL is that some old librarys may use (void*)0 as the definition, if it does then int* p = NULL; wouldn't compile in C++, as there is no implicit conversion from void* to int* like there is in C.
Quote:Original post by Programmer16
Oh, and since the topic is up, if NULL is just a macro, how come it is better to use it instead of 0?


Who said it's better?

See what Bjarne has to say about it:
Quote:Should I use NULL or 0?
In C++, the definition of NULL is 0, so there is only an aesthetic difference. I prefer to avoid macros, so I use 0. Another problem with NULL is that people sometimes mistakenly believe that it is different from 0 and/or not an integer. In pre-standard code, NULL was/is sometimes defined to something unsuitable and therefore had/has to be avoided. That's less common these days.
It does help make the code a bit more readable though. If you set a variable to NULL, then to anyone reading that code, the implication is that the variable is a pointer. Obviously you could misuse it and set an int variable to NULL, but if you're doing things like that then you probably need more than a "#define NULL 0L" to make your code readable. ;)

This topic is closed to new replies.

Advertisement