Home  >  Article  >  Backend Development  >  Why use zero address as null pointer in C/C++?

Why use zero address as null pointer in C/C++?

WBOY
WBOYforward
2023-09-03 12:53:06665browse

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.

Grammar

Float *p = 0 //initializing the pointer as NULL.

Algorithm

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.

Example:

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;
}

Output

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!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete