# NULL Undeclared !?

This topic is 4273 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

##### Share on other sites
>How come NULL is undeclared !?

Well, obviously nobody defined nor declared NULL. It's not a keyword.

##### Share on other sites

NULL is defined for example in string.h.

NULL isn't a compiler keyword just a #define NULL 0L (or something similar).

Some people even say that using NULL is bad, although I find it a bit clearer.

Cheers

##### Share on other sites
You could just do

#ifndef NULL
#define NULL 0
#endif

then your code won't break if you put a header in later that defines it first.

I thought it was okay to just use 0 in C++ though. I always do.

##### Share on other sites
Just a tip, get yourself the Visual C++ 2003 Toolkit (pluggable into VC6) or Visual C++ 2005 Express, both free downloads at the microsoft site.

Visual C++ 6 is OLD, BUGGY and so NON STANDARDS COMPLIANT that I can't call it C++ compiler anymore without blushing.

##### Share on other sites
NULL is declared in alloc.h, mem.h, stddef.h, stdio.h and stdlib.h :)

Personally I'd include stddef.h :)

##### Share on other sites
Just keep in mind that all the .h headers are C headers, e.g. it is not C++ you are writing, but some pre-standard form of it. The C++'ized C headers are prefixed with 'c' and don't have a '.h' suffix, e.g. <cstddef> instead of <stddef.h>.

##### Share on other sites
Doesn't really matter what standard header you include. Most of them define null or include something else that does.

##### Share on other sites
Quote:
 Original post by DeyjaDoesn't really matter what standard header you include. Most of them define null or include something else that does.

It depends.

If you want to write code that complies with the C++ standard as defined in the ISO documents (eg. ISO/IEC 14882:2003(E)) so that it is portable and uses strictly defined behaviour, thus minimizing both your development costs and porting/maintenance costs, you would include the header that is specified for whatever symbol you need (eg. for NULL you must #include <cstdlib>, and NULL must be a preprocessor macro that resolves to an rvalue of type pointer to p, yadda yadda yadda).

If all you want to do is get things working on whatever version of tool supplied by a particular vendor on the machine in front of you at the moment, then no it doesn't matter as long as it compiles. Heck, you don't even have to run your software to test it, as long as it compiles.

You can just choose to strive for prefessional quality. The professionals will curse you less when they are forced to fix your code if you do.

##### Share on other sites
Who cares? Only kids out of college use NULL. Real programmers use 0.

##### Share on other sites
<<Who cares? Only kids out of college use NULL. Real programmers use 0.>>

Blink. I avoid using any number where ever possible as 0 can mean true or false or success or failure or valid or invalid... Though I've been told I'm wierd before...

##### Share on other sites
I typecast NULL as NOTHING and then define VOID, NADA, BLANK as NOTHING... just to make it even more confusing for noobs ;)

and btw: real programmers program in 1's and 0's so that they never have to deal with stupid name associations.

##### Share on other sites
visual studio 2005 professional has:

#define NULL (-1)

##### Share on other sites
Quote:
 Original post by Anonymous Postervisual studio 2005 professional has:#define NULL (-1)

In which header? -> Click Me.

##### Share on other sites
It's either a very bad joke or a troll.

From stdlib.h (which is included by cstdlib):
/* Define NULL pointer value */#ifndef NULL#ifdef __cplusplus#define NULL    0#else#define NULL    ((void *)0)#endif#endif

##### Share on other sites
Thanks for the enlightenment, dudes! ;)

Looking back, it turns that it wasn't strange at all. I'd say never listen to your lecturer.
When I first started learning C, I guessed NULL was declared/defined in "stdio.h". But NULL was being used just about everywhere, in every book, in every tutorial, whether it was DOS or Windows. So I thought "windows.h" was either including something from the standard libs, or it declares NULL on its own. But I never excluded the possibility that NULL could be "internally declared/defined". Whatever... The thing is that NULL has to be some value, and what would it be if not 0?

Sometime later I get to learn this at university, and the lecturer says: "NULL is not the same as 0". (Or at least I misunderstood). So if it's not the same, than it must be internally declared, so the compiler knows it as a pointer.

And now, for the first time in 4 years I get this "strange" error message. ;) Apparently I REM'ed out the include directive to see if I can kick the error messages regarding the template. Off to download the newer version of VC. Hopefully it doesn't require Windows XP.

Thanks again!

##### Share on other sites
Maybe your lecturer meant the NULL in C and in C it's not necessarily 0 oder (void*)0. The value is implementation defined.
But in c++ 0 and NULL are the same.

##### Share on other sites

This topic is 4273 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628735
• Total Posts
2984449

• 25
• 11
• 10
• 16
• 14