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

## Recommended Posts

If NULL is defined as 0 would it be wrong to set pointers to 0 instead of NULL?

##### Share on other sites
No:

#define NULL 0
char* tempPtr = NULL;

The line: char* tempPtr = NULL; is then char* tempPtr = 0; after the preprocessor substitues in 0 for NULL. The point of the define is to remap one symbol/expression into something else. In this case, NULL and 0 are the same thing.

##### Share on other sites
Quote:
 Original post by FlyingDemonIf NULL is defined as 0 would it be wrong to set pointers to 0 instead of NULL?

It isn't necessarily *wrong* it's just the same thing.

##### Share on other sites
Quote:
 Original post by Drew_BentonNo:#define NULL 0char* tempPtr = NULL;The line: char* tempPtr = NULL; is then char* tempPtr = 0; after the preprocessor substitues in 0 for NULL. The point of the define is to remap one symbol/expression into something else. In this case, NULL and 0 are the same thing.

Actually in C, NULL is defined as :

#define NULL (void*)0

In c++ it's basically 0.

##### Share on other sites
NULL is just absence of a value. I know that in Java, NULL is actually infact, absent of a value. In C/C++ you just have to settle with 0 really, although if the implementation changed (for whatever daft reason), you could just redefine NULL to be the real implementation of NULL.

##### Share on other sites
Ok. I was just woundering because everytime I needed to use NULL I had to include a header that defined it like stdlib or define it myself.

##### Share on other sites
Quote:
 Original post by FlyingDemonOk. I was just woundering because everytime I needed to use NULL I had to include a header that defined it like stdlib or define it myself.

Well NULL is also just a convention to use, so don't include more header files just to get that. Are you using NULL for any reason in particular?

##### Share on other sites
Quote:
 Original post by FlyingDemonOk. I was just woundering because everytime I needed to use NULL I had to include a header that defined it like stdlib or define it myself.
stddef.h is the standard header if you only need NULL (or size_t/ptrdiff_t/offsetof).

Additionally GCC checks for use of NULL in non-pointers contexts so it basically acts the same way as a (void *) 0 definition would in C, but with warnings instead of errors as the result of any abuse.

It's mostly a matter of style/taste however. So if you're uncomfortable with having to include it or just think 0 looks better then feel free to switch.
Personally I prefer to use NULL.

##### Share on other sites
I'm only using NULL to set pointers to NULL after they no longer point to other things, and after de-allocating memory.

##### Share on other sites
I still prefer Scott Meyer's* Templatized-Null-On-Crack™.

Enigma

*It may well not be his invention, but it was his book which introduced me to the concept.

##### Share on other sites
Quote:
 Original post by EnigmaI still prefer Scott Meyer's* Templatized-Null-On-Crack™.
Or you could just get GCC an save yourself some typing..
Well, it won't work for overloaded argument types but that's exceedingly rare in practice and you'll at least get a warning.

##### Share on other sites
Quote:
 Original post by GhostBlazerActually in C, NULL is defined as :

No, in Standard C it's an implementation defined null pointer constant. It could be (void *)0 or it could be just 0.

##### Share on other sites
NULL's definition is a convienience, but according to specification, 0 is defined as "no target", and according to hardware, might not be physically a 0. For instance, if you have a machine where 0xffffffff is the no target for pointers, then ptr=0; will, according to C specification, assign 0xffffffff to ptr.

But yeah, NULL is usually 0 or (void*)0. I just use 0 personally.

##### Share on other sites
In C++ you are supposed to use 0 (an implicit cast from 0 to any pointer type is required by the standard, this is different than C and is a special case just for 0).

In C you are supposed to use NULL because, as mentioned, it explicitly cast 0 to the correct pointer type. Theoretically it doesn’t have to be the integer value 0 either.

##### Share on other sites
Quote:
 Original post by FlyingDemonIf NULL is defined as 0 would it be wrong to set pointers to 0 instead of NULL?

Don't go there man, just don't go there! (Damn I'm too late[smile])
There are plenty of existing topics that discuss the issue, if you'd like to search the site.

##### Share on other sites
i dont think it means anything but off hand setting something to 0 is in fact setting it to nothing unless it was an int, setting to NULl actually to me sounds like u are setting it to nothing . . i dont know