georgiana_e
georgiana_e

Reputation: 1869

The null-pointer-constant is not specified using the NULL macro

I am using a tool for analysing c++ code. My code looks something like this:

#define NULL 0

...
char * buff;
if (buff != NULL) { // -> error The null-pointer-constant is not specified using the NULL macro
 ...
}

Update: If I delete the #define null line I am getting the same error on this:

const int* var = 0; 

Do you have any idea why this syntax doesn't work, It is because NULL is defined as 0?

Thanks

Upvotes: 0

Views: 391

Answers (1)

Tiago Gomes
Tiago Gomes

Reputation: 386

The value for the NULL macro is implementation defined in C++.

With that in mind, it's reasonable that your static analysis tool is complaining about you using a value defined by yourself which may be wrong. ( even if it will work with basically all compilers )

That being said, if you have access to C++11, the preferred way is using the keyword nullptr instead of NULL as it solves most of the problems with the using NULL or 0.

Example ( live code: http://ideone.com/EmahXG ):

int foo( int   );
int foo( int * );

foo( NULL    ); // Will call first overload if NULL is defined as 0
foo( nullptr ); // Will call second overload as nullptr is not implicitly convertable to non-pointer types.

Upvotes: 2

Related Questions