Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do we check if a pointer is NULL pointer?

Tags:

c

null-pointer

I always think simply if(p != NULL){..} will do the job. But after reading this Stack Overflow question, it seems not.

So what's the canonical way to check for NULL pointers after absorbing all discussion in that question which says NULL pointers can have non-zero value?

like image 819
cpuer Avatar asked May 31 '11 09:05

cpuer


People also ask

How do you check if something is a null pointer?

So that if p is a null pointer then it must compare equal to any null pointer including NULL , in which case p != NULL would evaluate to false. Conversely, if p points to an object or function then it must compare unequal to any null pointer in which case p != NULL would evaluate to true.

Can a pointer to a pointer be null?

Hence when a pointer to a null pointer is created, it points to an actual memory space, which in turn points to null. Hence Pointer to a null pointer is not only valid but important concept.

How is a null pointer represented?

The preprocessor macro NULL is defined as an implementation-defined null pointer constant, which in C99 can be portably expressed as ((void *)0) which means that the integer value 0 converted to the type void* (pointer to void).


1 Answers

I always think simply if(p != NULL){..} will do the job.

It will.

like image 170
cnicutar Avatar answered Oct 18 '22 14:10

cnicutar