How to check if void pointer points to NULL?

pyrrhic picture pyrrhic · Aug 18, 2013 · Viewed 63.1k times · Source

So if you do:

void *ptr = NULL;

What is the best way to check if that void pointer is NULL?

My workaround for now is this:

if (*(void**)ptr == NULL) ... 

But this doesn't seem like the best way, as I'm implicitly assuming ptr is of type void** (which it isn't).

Answer

nmaier picture nmaier · Aug 18, 2013

I'd simply write if (!ptr).

NULL is basically just 0 and !0 is true.