Home > Backend Development > C++ > body text

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

WBOY
Release: 2023-09-03 12:53:06
forward
670 people have browsed it

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.
Copy after login

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.
Copy after login

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;
}
Copy after login

Output

The value of pointer is 0.
Copy after login

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!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template