Testing for a NULL Pointer - C Pointer

C examples for Pointer:NULL Pointer

Introduction

Suppose that you create a pointer like this:

int *pvalue = NULL;

NULL is a special symbol in C that represents the pointer equivalent to 0 with ordinary numbers.

The symbol is often defined as ((void*)0).

When you assign 0 to a pointer, it's the equivalent of setting it to NULL, so you could write the following:

int *pvalue = 0;

Because NULL is the equivalent of zero, if you want to test whether pvalue is NULL, you can write this:

if(!pvalue)
{
  //the pointer is NULL! . . .
}

When pvalue is NULL, !pvalue will be true, so the block of statements will be executed only if pvalue is NULL.

Alternatively, you can write the test as follows:

if(pvalue == NULL)
{
  // the pointer is NULL! . . .
}

Related Tutorials