Home >Backend Development >C++ >Why use zero address as null pointer in C/C++?
A null pointer is a pointer that does not point to anything.
Some uses of null pointers:
b) Used to initialize a pointer variable when the pointer variable has not yet been assigned any valid memory address.
b) Pass null pointer to function parameter when we don't want to pass any valid memory address.
c) Check for a null pointer before accessing any pointer variable. This way we can have error handling in pointer related code such as dereferencing a pointer variable only if it is not null.
In C, if we assign 0 to any pointer, then the pointer points to NULL.
Float *p = 0 //initializing the pointer as NULL.
Begin. Declare a pointer p of the integer datatype. Initialize *p= NULL. Print “The value of pointer is”. Print the value of the pointer p. End.
Live demonstration
#include <stdio.h> int main() { int *p= NULL;//initialize the pointer as null. printf("The value of pointer is %u",p); return 0; }
The value of pointer is 0.
The above is the detailed content of Why use zero address as null pointer in C/C++?. For more information, please follow other related articles on the PHP Chinese website!