Table of contents
1.
Introduction 
2.
Syntax of Void Pointer
3.
Dereferencing a Void Pointer
4.
Arithmetic Operation on Void Pointers
5.
Advantages of Void Pointer
6.
FAQs
7.
Key Takeaways
Last Updated: Mar 27, 2024

Void Pointer

Author Shivam Verma
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction 

A void pointer in C language is a pointer that does not have any associated data type. Void pointers are also known as generic pointers. These pointers can be used to point to the memory address of any variable irrespective of their data type. But you have to do typecasting to get a value.

Also Read About, Sum of Digits in C, C Static Function

Syntax of Void Pointer

The syntax of declaration of a void pointer is-

void *pointer_name;  

In the above syntax, void indicates that the pointer is a void pointer, indicates that the variable is a pointer variable, and pointer_name is the pointer's name.

Example 

void *vp;

In the above example, the void is the pointer type, and 'vp' is the pointer's name.

Let us take an example of declaring and initializing void pointer in C:

int i=10;
char ch='a';
void *vp= &i;
vp=&ch;

In the above example, we notice that vp is a void pointer; we can make it point to a char type variable and an int type variable.

Dereferencing a Void Pointer

A void pointer cannot be dereferenced simply by using the indirection operator. Here is an example.

#include <stdio.h>  
int main()  
{  
    int i=15;  
    void *ptr;
    ptr=&i;  
    printf("%d",*ptr);
    return 0;  
} 
You can also try this code with Online C Compiler
Run Code

Compilation of the above program will give a compile-time error because we already know that the void pointer cannot be dereferenced directly.

Here, we removed the compile-time error by typecasting the void pointer to the integer pointer.

#include <stdio.h>  
int main()  
{  
    int i=15;  
    void *ptr;
    ptr=&i;  
    printf("%d",*(int*)ptr);
    return 0;  
}
You can also try this code with Online C Compiler
Run Code

Output 

15

The above program compiles and runs fine.

Arithmetic Operation on Void Pointers

As we saw before, a void pointer cannot be dereferenced directly. Similarly, pointer arithmetic cannot be performed on void pointers without typecasting.

Example

#include<stdio.h>
int main()
{
    float arr[3]={1.5,2.3,5.4};
    void *ptr;
    ptr=arr;
    for(int i=0;i<3;i++)
    {
       printf("%.1f\n", *(float *)ptr);
       ptr=(float*)ptr+1; // cannot write ptr=ptr+1.
    }
    return 0;
}
You can also try this code with Online C Compiler
Run Code

Output

1.5
2.3
5.4

Here the above program does not give any error as we applied the proper typecasting to the void pointer, i.e., (float*)ptr. Then we applied the arithmetic operation, i.e., ptr=((float*)ptr+1).

You can also read about the dynamic arrays in c, And Tribonacci Series

Advantages of Void Pointer

  • The malloc() and calloc() function return void * or generic pointer, so we can use these functions to allocate memory of any data type.
    Example
#include <stdio.h>  
#include<malloc.h>  
int main()  
{ 
     int i=15;  
     int *ptr=(int*)malloc(sizeof(int));  
     ptr=&i;  
     printf("Value pointed at by ptr: %d",*ptr);
     return 0;  
}
You can also try this code with Online C Compiler
Run Code

Output

Value pointed at by ptr: 15
  • The void pointer in C can be used to implement the generic functions in C.

 

Read about Bitwise Operators in C  and  Short int in C Programming

FAQs

  1. What is the void pointer in C?
    A void pointer in the C language is a pointer that does not have any associated data type.
     
  2. How can we dereference the void pointer?
    To dereference a void pointer, we must typecast it to a valid pointer type.
     
  3. What is the correct syntax of a void pointer?
    The correct syntax of a void pointer is void *pointer_name;.
     
  4. What is the size of the void pointer?
    The size of the void pointer depends on your platform.
    On a 64-bit platform, you need 8 bytes to store a memory address.
    On a 32 bit platform, you need 4 bytes to store a memory address
    On a 16-bit platform, you need 2 bytes to store a memory address.

Key Takeaways

In this article, we have extensively discussed the void pointers topic and their implementation in the C programming language. We hope that this blog has helped you enhance your knowledge, and if you wish to learn more, check out our article on pointers and visit our library of curated blogs by clicking here. Do upvote our blog to help other ninjas grow. Happy Coding!

Live masterclass