Home >Backend Development >C++ >Null pointer in C

Null pointer in C

WBOY
WBOYforward
2023-09-09 13:21:09806browse

Null pointer in C

The void pointer in C is a pointer that is not associated with any data type. It points to some data location in storage, meaning the address of a variable. It is also called a universal pointer. In C language, the malloc() and calloc() functions return void * or a general pointer.

It has some limitations -

1) Due to void pointers, pointer arithmetic cannot use the specific size of void pointers.

2) It cannot be used as a dereference.

Algorithm

Begin
   Declare a of the integer datatype.
      Initialize a = 7.
   Declare b of the float datatype.
      Initialize b = 7.6.
   Declare a pointer p as void.
   Initialize p pointer to a.
   Print “Integer variable is”.
      Print the value of a using pointer p.
   Initialize p pointer to b.
   Print “Float variable is”.
      Print the value of b using pointer p
End.

This is a simple example -

Sample code

Real-time demonstration

#include<stdlib.h>
int main() {
   int a = 7;
   float b = 7.6;
   void *p;
   p = &a;
   printf("Integer variable is = %d", *( (int*) p) );
   p = &b;
   printf("\nFloat variable is = %f", *( (float*) p) );
   return 0;
}

Output

Integer variable is = 7
Float variable is = 7.600000

The above is the detailed content of Null pointer in 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