What is a null pointer in C?

by naval| Views: 939

What is use of NULL Pointer in C Programming? How to get NULL Pointer in our Program?



Tags:  C Interview

Answers (1)
 
Zulfiqar Said..

"A null pointer is a special pointer value that is known not to point anywhere. What this means that no other valid pointer, to any other variable or array cell or anything else, will ever compare equal to a null pointer"

There are times when it’s necessary to have a pointer that doesn’t point to anything. The macro NULL, defined in , has a value that’s guaranteed to be different from any valid pointer. NULL is a literal zero, possibly cast to void* or char*. Some people, notably C++ programmers, prefer to use 0 rather than NULL.

The null pointer is used in three ways:
1) To stop indirection in a recursive data structure
2) As an error value
3) As a sentinel value

The most straightforward way to "get" a null pointer in your program is by using the predefined constant NULL, which is defined for you by several standard header files,including "stdio.h", "stdlib.h", and "string.h"

To initialize a pointer to a null pointer, you might use code like


#include

int *ip = NULL;

and to test it for a null pointer before inspecting the value pointed to you might use code like

if(ip != NULL)
printf("%d\n", *ip);



Register or Login to Post Your Opinion/Answer