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

In C language, what is a null pointer?

王林
王林forward
2023-09-24 13:53:091130browse

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;

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)

Example 1

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

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

Output

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

i = 10
f = 5.34

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

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

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!

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