Skip to main content
gamedev.net
🔒 Locked

'NULL' : undeclared identifier

Started by emileej 12 replies 2.1k views
Original Post
emileej
emileej
//...

	int *ival;
//...

	ival=NULL;
//...

	bool IsInt(){ return (ival!=NULL); } //error C2065: ''NULL'' : undeclared identifier

//...

Now why - oh - why is this so? ------------- E-)mil "I feel like... I feel like... Like taking over the world!" - Day of the Tentacle
Emil Johansen- SMMOG AI designerhttp://smmog.com
foofightr
foofightr
NULL is a #define in some headers, it is not built into any compiler. The most lightweight way is to copy it from a standard header into your own header. For example:


// the almighty NULL, shamelessly ripped from windef.h
#ifndef NULL
# ifdef __cplusplus
# define NULL 0
# else
# define NULL ((void *)0)
# endif
#endif


[edited by - foofightr on July 15, 2003 5:46:10 AM]
JY
JY
At a guess, because you don''t have the header file included that defines NULL?

NULL is a preprocessor definition which is #define''d to 0.

#define NULL 0
"Absorb what is useful, reject what is useless, and add what is specifically your own." - Lee Jun Fan
emileej
emileej
The thing is: If I make a very simple program:

int main(int argc, char argv[]){
void *ptr;
ptr=NULL;
return 0;
}

...all is ok. Wierd!
Emil Johansen- SMMOG AI designerhttp://smmog.com
Qw3r7yU10p!
Qw3r7yU10p!
are you sure that example has no headers included?

also what compiler and libraries are you using?
emileej
emileej
I am using MS VC++ 6 - just created an empty workspace and added main.cpp with the code you see - none else.
Emil Johansen- SMMOG AI designerhttp://smmog.com
Fruny
Fruny
#include <cstddef> or just use 0 instead.


[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Qw3r7yU10p!
Qw3r7yU10p!
i tried exactly that and it came up with:

Compiling...
null.cpp
c:\work\sand\xor\null.cpp(4) : error C2065: ''NULL'' : undeclared identifier

I have VC6.0 SP5

are you sure there are no includes (such as stdafx.h) or anything else?
emileej
emileej
Positive, but ill just use
0 in stead - it seems simpler.
Emil Johansen- SMMOG AI designerhttp://smmog.com
Qw3r7yU10p!
Qw3r7yU10p!
I can''t believe that code is compiling.

Try compiling using ctrl-f7

Is it included in the project? Can you run it? If so place a breakpoint in the code. Rebuild it with browse information, right click on NULL and choose go to definition - what does it give you?
foofightr
foofightr
Don''t use 0 instead of NULL just because you''re lazy. The whole point of NULL is to help others (and yourself in a few months) be able to distinguish integers from pointers at a quick glance. 0 is for integers, NULL is for pointers. That''s the whole point.
Trienco
Trienco
or use neither.. if (ptr) instead of if (ptr!=NULL) or if (!ptr) instead of if (ptr==0)... wont make it any more readable but you can be even more lazy. btw. stroustrup himself recommends to use 0 instead of NULL for fewer problems or at least defining it as const int and not via #define.

someone looking at the code should be able to recognize a pointer even without having any NULL around if you name your variables correctly.
f@dzhttp://festini.device-zero.de

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.