Monday, May 24, 2010

Null Pointers

A null pointer constant is an integer constant expression with the value 0, or such an expression cast as the type void *. The macro NULL is defined in stdlib.h, stdio.h and other header files as a null pointer constant.

#include /* ... */
FILE *fp = fopen( "demo.txt", "r" );
if ( fp == NULL )
{   // Error: unable to open the file demo.txt for reading.
}

Null pointers are implicitly converted to other pointer types as necessary for assignment operations, or for comparisons using == or !=. Hence no cast operator is necessary in the previous example. A null pointer is what results when you convert a null pointer constant to a pointer type.

Source: C in a Nutshell - O'Reilly Media

No comments:

Post a Comment