Void Pointer in C

Void Pointer in C Language with Examples

In this article, I am going to discuss Void Pointer in C Language with Examples. Please read our previous articles, where we discussed the Null Pointer in C Language with Examples.

Void Pointer in C:

The Generic pointer of C & C++ is called a void pointer. A generic pointer means it can access and manipulate the data of any kind of variable. The Size of the void pointer is 2 bytes. By using a void pointer, when we are accessing the data then it is mandatory to use the Typecasting mechanism else it will give you an error. When we are working with the void pointer, type specification will be decided at run time only. When we are working with the void pointer, arithmetic operations are not allowed, i.e. incrementation and decrementation of the pointer are restricted.

Program to understand Void Pointer in C Language:

Let us understand the need for a void pointer with an example. In the below program, we have created three different types of pointers i.e. integer, character, and float pointers. So, in the below example, we are not using the void pointer and this is the reason why we created three-pointers.

#include<stdio.h>
void main ()
{
    int i;
    float f;
    char ch;
    int *iptr = (int *) 0;
    float *fptr = (float *) 0;
    char *cptr = (char *) 0;
    iptr = &i;
    i = 10;
    printf ("\n %d %d ", i, *iptr);
    fptr = &f;
    f = 12.8;
    printf ("\n %d %d", f, *fptr);
    cptr = &ch;
    ch = 'A';
    printf ("\n %d %d", ch, *cptr);
}
Output:

Program to understand Void Pointer in C Language

Let us see, how we can replace the previous program with the void pointer. That means, in place of constructing 3 types of pointers, we can create a single pointer variable that can access and manipulate any kind of variable properly, i.e. void pointer. As the void pointer can access and manipulate any kind of data variables, so it is also called a Generic Pointer.

Program using Void Pointer:
#include<stdio.h>
void main ()
{
    int i;
    float f;
    char ch;
    void *ptr;
    ptr = &i;
    *(int *) ptr = 10;
    printf ("\n %d %d", i, *(int *) ptr);
    ptr = &f;
    *(float *) ptr = 12.8;
    printf ("\n %f %f", f, *(float *) ptr);
    ptr = &ch;
    *(char *) ptr = 'A';
    printf ("\n %c %c", ch, *(char *) ptr);
}
Output:

Program using Void Pointer

Note: A Void Pointer in C Programming Language is a pointer that has no associated data type with it and this is the reason why a void pointer in C programming language can hold the address of any data type and can be typecasted to any data type as well.

Size of the Void Pointer:

The size of the void pointer in the C programming language is the same as the size of the pointer of the character datatype. According to the C language standard, the representation of a pointer to void is the same as the pointer of character datatype. The size of the pointer will vary depending on the platform that we are using. For a better understanding, please have a look at the below example.

#include <stdio.h>
int main ()
{
    void *VP = NULL;		//Void Pointer  
    int *IP = NULL;		//Integer Pointer  
    char *CP = NULL;		//Character Pointer  
    float *FP = NULL;		//Float Pointer  

    printf ("Size of Void Pointer = %d\n\n", sizeof (VP));
    printf ("Size of Integer Pointer  = %d\n\n", sizeof (IP));
    printf ("Size of Character Pointer = %d\n\n", sizeof (CP));
    printf ("Size of Float Pointer = %d\n\n", sizeof (FP));

    return 0;
}
Output:

Size of the Void Pointer in C Language

Points to Remember:

1) The void pointers cannot be dereferenced. For example, the below program will not compile.

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

When you run the above code, you will get the following error.

Points to Remember while working with Void Pointers in C Language

The following program compiles as well as runs fine.

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

Output: 20

Advantages of Void Pointers:

As we already discussed in our previous articles, the malloc() and calloc() built-in functions in C Language return void pointer (void *) and this is the reason why these two functions are used to allocate memory of any data type.
int main(void)
{
       // The malloc() function returns void * which can be typecasted to any type like int *, char *, ..
       int *x = malloc(sizeof(int) * n);
}

The void pointers in the C programming language are used to implement generic functions in C and which we already discussed at the beginning of this article.

In the next article, I am going to discuss Dangling Pointer in C language. Here, in this article, I try to explain Void Pointer in C Language with Examples. I hope you enjoy this article. I would like to have your feedback. Please post your feedback, question, or comments about this article.

Leave a Reply

Your email address will not be published.