In regard to good style, is it better to use the NULL macro, or should you just use 0? And should you cast either one to the type of pointer you're using?

i.e.:

int* pInt = (int*)0;//this,
int* pInt2 = 0;//this,
int* pInt3 = (int*)NULL;//this,
int* pInt4 = NULL;//or this?

If I recall correctly, all pointers (of all types) can be assigned the address of the NULL type (or zero).

I don't see why a cast is necessary.

I don't know about using NULL vs 0, but I guess if NULL is a macro and you're worried about it not being defined somehow, I'd just put an #ifdef, #undef and #ifndef condition for defining the macro to be zero.

A cast isn't necessary. To my knowledge, all four of the example I gave are valid. I was wondering if any one of them is considered better than another for style purposes, or if there even is a unanimous (or unanimous-ish) agreement on which is better.

A cast isn't necessary. To my knowledge, all four of the example I gave are valid. I was wondering if any one of them is considered better than another for style purposes, or if there even is a unanimous (or unanimous-ish) agreement on which is better.

My two cents:

int* pInt = (int*)0;//this,
int* pInt2 = 0;//this,
int* pInt3 = (int*)NULL;//this,
int* pInt4 = NULL;//or this?

The bottom one is preferred, the second one is perfectly fine, and the first and third are valid C++ code, but shouldn't be used. Nothing you use is ever going to be stored at address 0, so typecasting something that will never be used seems silly, so pick between the second and the fourth. My vote is the fourth, but I don't think it really matters.

In c++ NULL is defined to be 0. There is no general agreement about whether to use NULL or 0 for pointers

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.

Some additions:
1. Of course, all four declarations are legal in C++ (I prefer the simplest 2nd - it's the only form in pure C++ w/o C++ library headers ;)). C++ (pre)Standard (4.1):

A null pointer constant is an integral constant expression (5.19) rvalue of integer type that evaluates to zero. A null pointer constant can be converted to a pointer type; the result is the null pointer value of that type and is distinguishable from every other value of pointer to object or pointer to function type. Two null pointer values of the same type shall compare equal.

Fanny result (don't try to repeat w/o special training ;)) - it's legal C++ declaration:

int* pNowhere = 2*2 - 4;

See also brief discussion on this topic:
http://www.velocityreviews.com/forums/t284567-null-macro-vs-0-as-null-pointer.html

Waiting for nullptr keyword...

Just say NULL, because it is the most clear. People who use anything else are idiots, trying to be clever.

commented: Lol! =) +2

To sarehu: thank you for your kindness.
Yes, I'm an idiot (and many others too).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.