Home > Backend Development > C++ > In C language, what is a null pointer?

In C language, what is a null pointer?

王林
Release: 2023-09-24 13:53:09
forward
1134 people have browsed it

In C language, what is a null pointer?

It is a pointer that can save the address of any data type variable (or) can point to any data type variable.

Declaration

The declaration of void pointer is as follows −

void *pointername;
Copy after login

For example − void *vp;

Access − Through pointer Type conversion operators are used when accessing the value of a variable.

Grammar

The syntax of void pointer is as follows −

* ( (type cast) void pointer)
Copy after login

Example 1

int i=10;
void *vp;
vp = &i;
printf ("%d", * ((int*) vp));
// int * type cast
Copy after login

Example

The following is the C program void pointer:

Real-time demonstration

#include<stdio.h>
main ( ){
   int i =10;
   float f = 5.34;
   void *vp;
   vp = &i;
   printf ("i = %d", * ((int*)vp));
   vp = &f;
   printf ( "f = %f", * ((float*) vp));
}
Copy after login

Output

When the above program is executed, it produces the following results−

i = 10
f = 5.34
Copy after login

Example 2

Given below is a C program for pointer arithmetic in null pointers −

Online Demonstration

#include<stdio.h>
#define MAX 20
int main(){
   int array[5] = {12, 19, 25, 34, 46}, i;
   void *vp = array;
   for(i = 0; i < 5; i++){
      printf("array[%d] = %d</p><p>", i, *( (int *)vp + i ) );
   }
   return 0;
}
Copy after login

Output

When the above program is executed, it produces the following Result−

array[0] = 12
array[1] = 19
array[2] = 25
array[3] = 34
array[4] = 46
Copy after login

The above is the detailed content of In C language, what is a null pointer?. 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