I need help with one simple operator

Ok i am a programmer in Lua for some game sites; and in Lua nil means something isn't defined correctly; therefor it doesn't exist <-- In other words Nil means nothing

In C++ there is Null now isn't Null like nil?
Oh, and please show a good example of how this operator works please
NULL is just a macro (defined in WinAPI, I think) which resolves to zero:
#define NULL 0

You can have null pointers, i.e. pointers which are 0/NULL (and don't point to anything). But a non pointer variable cannot be null.

1
2
3
4
5
6
7
8
9
10
11
12
#include <string>

int main()
{
   std::string str;
   str = 0; // try to set a string to 0, i.e. NULL, this generates a compiler error
   
   std::string* pStr;
   pStr = 0; // this is OK: we have set a pointer to null

   return 0;
}


Hope this helps.
Yea i get it Null is 0 and can only work with pointers NOT non pointers <-- Right?
That is correct, as far as I know. (Of course, you could set, e.g. an integer, to 0, but this is notionally different from the NULL 0 used for pointers...)

And if a pointer is NULL/0, it means it doesn't point to anything. That is, it is a bad pointer and can't be dereferenced, etc.
Actually NULL is defined 0 in C++. In C it is defined as void reference to the address 0:
NULL (void*)0
This is not a significant difference, it is like "0" and "-0" for numbers, but it would give a better understanding why NULL is only for pointers (because it is declared as a pointer).

edit:
The way in which is declared NULL in stdio.h (MsVS2005):
1
2
3
4
5
6
7
8
/* Define NULL pointer value */
#ifndef NULL
#ifdef __cplusplus
#define NULL    0
#else
#define NULL    ((void *)0)
#endif
#endif 
Last edited on
Topic archived. No new replies allowed.