Home > Backend Development > C++ > body text

Null pointer in C

WBOY
Release: 2023-09-09 13:21:09
forward
794 people have browsed it

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

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

Output

Integer variable is = 7
Float variable is = 7.600000
Copy after login

The above is the detailed content of Null pointer in 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!