Differentiate Null Pointer and Void Pointer in C Language



The difference between a Null pointer and a Void pointer is that a Null pointer represents a value, while a Void pointer is a type identifier.

NULL Pointer

A null pointer indicates that it does not point to anything. If a pointer has no assigned address, it should be set to null. Each pointer type, such as int* or char*, can have a null pointer value.

The syntax is as follows ?

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

Example

Following is the C program for NULL pointer ?

#include <stdio.h>

int main() { 
   printf("TutorialPoint C Programming
"); int *p = NULL; // ptr is a NULL pointer printf("The value of pointer is: %p
", (void *)p); return 0; }

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, or general-purpose pointer, does not have an associated data type. It can hold the addresses of any data type. The syntax is as follows ?

void *<data type>

Example

Following is the C program for Void Pointer ?

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

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

10

Difference Between Null and Void Pointer

Below is a table that highlights the differences between null pointers and void pointers in c programming.

Null Pointer Void Pointer
Null represents an empty variable value A Void denotes a pointer with no initial size.
Null means never existed. Void means, it exists but is no longer in use.
This doesn't point to a valid location. This can point to any type of data.
Updated on: 2024-12-06T15:41:23+05:30

12K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements