Null Pointer
NULL Pointer is a pointer which is pointing to nothing. A null pointer in most programming languages means "no value". Null pointers represent conditions such as the end of a list of unknown length or the failure to perform some operation.
A null pointer is different from an uninitialized pointer: A null pointer will always compare unequal to any pointer that points to a valid object, while an uninitialized pointer might point anywhere. Uninitialized pointer may be equal to other, valid pointers; or it might compare equal to null pointers.
C
Two null pointers of any type are guaranteed to compare equal. The macro NULL is defined as an implementation-defined null pointer constant,which in C99 can be expressed as the integer value 0 converted implicitly or explicitly to the type void*.
Dereferencing the NULL pointer typically results in an attempted read or write from memory that is not mapped - triggering a segmentation fault. This shows up as program crash or be transformed into an exception that can be caught. There are, however, certain circumstances where this is not the case. For example, in x86-real mode, the address 0000:0000 is readable and usually writable, hence dereferencing the null pointer is a perfectly valid but typically unwanted action that may lead to undefined but non-crashing behaviour in the application.
NULL
As a matter of style, many programmers prefer not to have 0's scattered through their programs to represent pointers. Therefore, the preprocessor macro NULL is defined (by headers such as <stdio.h> and <stddef.h>) as a null pointer constant, typically 0 or ((void *)0). A programmer who wishes to make explicit the distinction between 0 the integer and 0 the null pointer constant can then use NULL whenever a null pointer is required.
#define NULL 0
Using NULL is a stylistic convention only; the preprocessor turns NULL back into 0 which is then recognized by the compiler, in pointer contexts, as before. In particular, a cast may still be necessary before NULL (as before 0) in a function call argument. The table under question 5.2 above applies for NULL as well as 0 (an unadorned NULL is equivalent to an unadorned 0). NULL is macro constant defined in following header files –
- stdio.h
- alloc.h
- mem.h
- stddef.h
- stdlib.h
NULL should be used only as a pointer constant. It is bad practice to use NULL to assign 0 value to a variable.
Example of Null pointer
#include <stdio.h>
int main()
{
int *ptr = NULL; //Best way to assign the pointer Null to a pointer variable. Works for any data type.
float *f_ptr = (float *)0; //Another way to assign the pointer Null to a pointer variable of type float.
char *c_ptr = '\0'; //Assigning the pointer Null to a pointer variable of type char.
double *d_ptr = (double *)0;//Another way to assign the pointer Null to a pointer variable of type double.
printf("The value of ptr is %u\n",ptr);
printf("The value of f_ptr is %u\n",f_ptr);
printf("The value of c_ptr is %u\n",c_ptr);
printf("The value of d_ptr is %u\n",d_ptr);
return 0;
}
Output
The value of ptr is 0
The value of f_ptr is 0
The value of c_ptr is 0
The value of d_ptr is 0
C++
In C++ the integer literal for zero has been traditionally preferred to represent a null pointer constant. However, C++11 has introduced an explicit nullptr constant to be used instead. Example
int* x = nullptr;
myclass* obj = nullptr;
Check for a Null pointer
#include <stdio.h>
int main()
{
int *ptr = NULL;
float *f_ptr = NULL;
float y=10.0;
f_ptr=&y;
if(ptr)
{
printf("ptr is not a null pointer.\n");
}
else
{
printf("ptr is a null pointer.\n");
}
if(f_ptr)
{
printf("f_ptr is not a null pointer.\n");
}
else
{
printf("f_ptr is a null pointer.\n");
}
return 0;
}
Output
ptr is a null pointer.
f_ptr is not a null pointer.
If all unused pointers are assigned a null value, accidental misuse of an uninitialized pointer can be avoided. Many times, uninitialized variables hold some junk values and it becomes difficult to debug the program.