Differentiate the NULL pointer with Void pointer in C language

CServer Side ProgrammingProgramming

The difference between Null pointer and Void pointer is that Null pointer is a value and Void pointer is a type.

NULL pointer

A null pointer means it is not pointing to anything. If, there is no address that is assigned to a pointer, then set it to null.

A pointer type, i.e., int *, char * each have a null pointer value.

The syntax is as follows −

<data type> *<variable name> = NULL;

For example,

int *p = NULL;
char *p = '\0';

Example Program

Following is the C program for NULL pointer −

 Live Demo

#include<stdio.h>
int main(){
   printf("TutorialPoint C Programming");
   int *p = NULL; // ptr is a NULL pointer
   printf("\n The value of pointer is: %x ", p);
   return 0;
}

Output

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

TutorialPoint C Programming
The value of pointer is: 0

Void Pointer

A void pointer is nothing but the one who does not have any data type with it. It is also called as a general purpose pointer. It can hold the addresses of any data type.

Thee syntax is as follows −

void *<data type>;

For example,

void *p;
int a; char c;

p = &a; //p changes to integer pointer as address of integer is assigned to it

p = &c; //p changes to character pointer as address of character is assigned to it

Example

Following is the C program for Void Pointer −

 Live Demo

#include<stdio.h>
int main(){
   int a = 10;
   void *ptr = &a;
   printf("%d", *(int *)ptr);
   return 0;
}

Output

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

10
raja
Published on 09-Mar-2021 11:12:03
Advertisements