What is Void Pointer in C? Syntax, Usage, & Advantages

By Priyanshu Vaish|Updated : November 2nd, 2022

A Void pointer in C is a pointer with no associated data type. Instead, it designates a particular storage location for data. This means that it points to the variable's address. It's also known as the general purpose pointer. The functions malloc() and calloc() in C return void * or generic pointers.

Void Pointer C PDF

The void pointer in C has some limitations that is pointer arithmetic is not possible with a void pointer due to its concrete size, and a void pointer in C can't be used as deference. In this article, we discuss about the void pointer in C, syntax, advantages, etc.

Download Complete C Programming and Data Structures Formula Notes PDF

Table of Content

What is Void Pointer in C?

Void pointer in C is a unique type of pointer that can point to any object's data type. A void pointer in C is a general-purpose pointer that can hold any data type's address and can be typecasted to any type. This type of pointer is essential for the GATE exam. A Void pointer has no datatype associated with it.

With such void pointers, memory allocation becomes very simple. Because they make the functions flexible enough to become appropriately allocated with bytes and memories, let's look at the C syntax for the void pointer.

Syntax of Void Pointer in C

void *pointer name;

The void keyword serves as the pointer type. In this case, it is followed by the pointer name to which the pointer type points and allocates the address location in the programming code. The name and type of pointer that supports any given data type are declared when a pointer is declared.

void *p

The pointer expects a void type, not an int, float, or anything else. It is indicated by the p pointer, which includes the * symbol. It indicates that this pointer has been declared and will be denoted in the future. To understand this better, questions on Pointer in C are provided.

Size of Void Pointer in C

In the C programming language, the size of the void pointer is the same as the size of the character datatype pointer. The C language standard states that a pointer to void has the same representation as a pointer to a character datatype. The pointer size will differ depending on the platform we're using. Please see the example below for a better understanding.

#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 Void pointer = 8

Size of Integer pointer = 8

Size of Character pointer = 8

Size of Float pointer = 8

Why is Void Pointer in C Useful?

One major advantage of the void pointers in C is that they can be 'reused'.  Due to this characteristic of the void pointer in C, the complexities of 'space' and 'time' can be reduced to a certain extent, which is very important in Embedded System design. Void Pointer in C is also used to formulate the questions in the GATE CSE question paper. They are very helpful when you want a single pointer to reference data of various types at various times.

Please take a look at the following simple code snippet to show the following simple code snippet to demonstrate how void pointers can be reused and thus reduce space complexity.

#include <stdio.h>

int main()

{

int I = 10;

char c = 'A';

float f = 12.34;

double d = 43.78;

//Declaring a generic pointer

void *vptr;

//Storing the address of the integer variable

vptr = &i;

printf("The value of t: %d\n", *(int*)vptr);

//storing the address of the character variable

vptr = &c;

printf("The value of c: %c\n", *(char*)vptr);

//storing the address of the float variable

vptr = &f;

printf("The value of f: %f\n", *(float*)vptr);

//storing the address of the double variable

vptr = &d;

printf("The value of d: %f\n", *(double*)vptr);

return 0;

}

Output

The value of t: 10

The value of c: A

The value of f: 12.340000

The value of d: 43.780000

Advantages of Void Pointer in C

The functions malloc() and calloc() return the void* type (void pointers). This enables us to use these methods to allocate memory of any data type. Candidates can expect the questions in the GATE CSE exam on various advantages of the void pointer in C.

Void pointers in C are used to implement generic functions in the C programming language. For instance, the qsort() function in the standard C library.

Important GATE Notes
Work Done By A ForceMotion Under Gravity
Dynamic ResistanceStatic Resistance
Ideal DiodeBettis Theorem
Work Done By A Constant ForceApplication Layer Protocols
Castiglia's TheoremPortal Frames

Comments

write a comment

FAQs on Void Pointer in C

  • Void pointer in C is a unique type of pointer that can point to any object's data type. A void pointer in C is a general-purpose pointer that can hold any data type's address and can be typecasted to any type. Void pointer has no datatype associated with it.

  • The size of a void pointer varies depending on the system. A void pointer in a 16-bit system is 2 bytes long. A void pointer is 4 bytes in size on a 32-bit system. A void pointer is 8 bytes in size on a 64-bit system.

  • We use void pointers in C because they are reusable. Void pointers can store any type of object, and we can retrieve any type of object by using the indirection operator with proper typecasting.

  • A void pointer in C is the pointer that can point to any type of object but has no idea what type of object it is pointing to. In order to perform indirection, a void pointer should be explicitly cast into another type of pointer. A null pointer is one that does not point to an address. A void pointer in C may be a null pointer.

  • A null pointer has the value NULL, which is typically 0, but is always a memory location that cannot be dereferenced. A void pointer points to void data. The word "void" does not mean that the data the pointer references are incorrect or that the pointer has been rendered useless.

Follow us for latest updates