Download the BYJU'S Exam Prep App for free GATE/ESE preparation videos & tests - Download the BYJU'S Exam Prep App for free GATE/ESE preparation videos & tests -

Void Pointer in C

A pointer in a program that isn’t associated with a data type is known as a void pointer in C. The void pointer points to the data location

The void pointer in C is a pointer that is not associated with any data types. It points to some data location in the storage. This means that it points to the address of variables. It is also called the general purpose pointer. In C, malloc() and calloc() functions return void * or generic pointers.

In this article, we will take a look at the Void Pointer in C and its uses according to the GATE Syllabus for CSE (Computer Science Engineering). Read ahead to learn more.

Table of Contents

What is a Void Pointer in C?

The void pointers refer to the pointers that have no data type associated with them. As the name suggests, the void pointer indicates that the pointer is basically empty- and thus, it is capable of holding any data type address in the program. Then, these void pointers that have addresses, can be further typecast into other data types very easily.

The allocation of memory also becomes very easy with such void pointers. It is because they make the functions flexible enough to be allocated with bytes and memories appropriately. Let us look at the syntax for the void pointer in C.

Syntax

void *name_of_pointer;

Here, the void keyword acts as the pointer type, and it is followed by the pointer name- to which the pointer type points and allocates the address location in the code. The declaration of a pointer happens with the name and type of pointer that supports any given data type. Let us take a look at an example to understand this better.

void *ptr

Here, the pointer is expecting a void type- not int, float, etc. It is pointed by the ptr pointer here that includes the * symbol. It denotes that this pointer has been declared, and it will be used for dereferencing in the future.

Why use a Void Pointer in C?

As we all know, the address that is assigned to any pointer must have the same data type as we have specified in the declaration of the pointer. For instance, when we are declaring the float pointer, this float pointer won’t be able to point to an int variable or any other variable. In simpler words, it will only be able to point to the float type variable. We thus use a pointer to void to overcome this very problem.

The pointer to void can be used in generic functions in C because it is capable of pointing to any data type. One can assign the void pointer with any data type’s address, and then assign the void pointer to any pointer without even performing some sort of explicit typecasting. So, it reduces complications in a code.

The Basic Algorithm

Here is how the void pointers work in a program. Consider this as the pseudocode for the program discussed ahead.

Begin

Declaring v of int data type.

Initializing v = 7.

Declaring w of float data type.

Initializing w = 7.6.

Declaring the pointer u as void.

Initializing the u pointer to v.

Print “The Integer variable in the program is equal to = ”.

Printing the value of v using the pointer u.

Initializing the u pointer to w.

Print “The Float variable in the program is equal to = ”.

Printing the value of w using the pointer u.

End.

Example Code

Let us take a look at an example of how we use the above void pointers in a code.

#include<stdlib.h>

int main() {

int v = 7;

float w = 7.6;

void *u;

u = &v;

printf(“The Integer variable in the program is equal to = %d”, *( (int*) u) );

u = &w;

printf(“\nThe Float variable in the program is equal to = %f”, *( (float*) u) );

return 0;

}

The output obtained out of the program would be:

The Integer variable in the program is equal to = 7

The Float variable in the program is equal to = 7.600000

Now, we will take a look at a few more examples to understand how we can use the void pointer in a C program.

Example #1

#include<stdio.h>

int main()

{

int b = 52;

void *p = &b;

printf(“%d”, *(int *)p);

return 0;

}

The output obtained out of this program would be:

52

Example #2

#include<stdio.h>

int main()

{

int a[3] = {5,9,7};

void *p = &a;

p = p + sizeof(int);

printf(“%d”, *(int *)p);

return 0;

}

The output obtained out of this program would be:

9

Example #3

#include<stdio.h>

void main()

{

char i=’c’;

float k=13.8;

int j=17;

void *n;

n=&a;

printf(“n%c”,*((char*)n));

n=&k;

printf(“n%f”,*((float*)n));

n=&j;

printf(“%d”,*((int*)n));

}

The output obtained out of this program would be:

c

3.800000

17

The Size of Void Pointers in C Language

The void pointer’s size in a program is similar to the size of the char data type pointer in C. But its size varies a lot on the basis of the platform on which we are using it in the code.

Let us take a look at an example to understand this better.

#include <stdio.h>

int main()

{

int *p = NULL; // the integer pointer in the program

void *ptr = NULL; // the void pointer in the program

float *fp = NULL; // the float pointer in the program

char *cp = NULL; // the character pointer in the program

// the size of the integer pointer in the program

printf(“the size of the integer pointer in the program = %d\n\n”,sizeof(p));

// the size of the void pointer in the program

printf(“the size of the void pointer in the program = %d\n\n”,sizeof(ptr));

// the size of the float pointer in the program

printf(“the size of the float pointer in the program = %d\n\n”,sizeof(fp));

// the size of the character pointer in the program

printf(“the size of the character pointer in the program = %d\n\n”,sizeof(cp));

return 0;

}

The output of the program mentioned above would be like this:

the size of the integer pointer in the program = 8

the size of the void pointer in the program = 8

the size of the float pointer in the program = 8

the size of the character pointer in the program = 8

Limitations of using Void Pointers in C

Here are a few limitations of using a void pointer in C:

  • We cannot use it dereferenced.
  • The arithmetic of pointers isn’t possible in the case of void pointers because of their concrete size.

Practice Problems on Void Pointer in C

1. Take a look at the following program in C and find out the output for the same:

#include<stdlib.h>

int main() {

int g = 83;

float h = 4.9;

void *m;

m = &h;

printf(“\nThe Float in this program is = %f”, *( (float*) m) );

m = &g;

printf(“The Integer in this program is = %d”, *( (int*) m) );

return 0;

}

Answer: The Float in the program is = 4.900000

The Integer in the program is = 83

2. Take a look at the following program in C and find out the output for the same:

#include<stdio.h>

int main()

{

int x = 73;

void *a = &x;

printf(“%d”, *(int *)a);

return 0;

}

Answer: 73

3. Take a look at the following program in C and find out the output for the same:

#include<stdio.h>

void main()

{

char a=’tweepadock’;

char b=’ubbidubbi’;

char c=’skank’;

float x=2.14;

float y=5.6;

float z=9.37;

int p=81;

int q=44;

int q=29;

void *g;

g=&a;

printf(“The character a in this program is = %c”,*((char*)g));

g=&b;

printf(“The character b in this program is = %c”,*((char*)g));

g=&c;

printf(“The character c in this program is = %c”,*((char*)g));

g=&x;

printf(“The float x in this program is = %f”,*((float*)g));

g=&y;

printf(“The float y in this program is = %f”,*((float*)g));

g=&z;

printf(“The float z in this program is = %f”,*((float*)g));

g=&p;

printf(“The integer p in this program is = %d”,*((int*)g));

g=&q;

printf(“The integer q in this program is = %d”,*((int*)g));

g=&r;

printf(“The integer r in this program is = %d”,*((int*)g));

}

Answer: The character a in this program is = tweepadock

The character b in this program is = ubbidubbi

The character c in this program is = skank

The float x in this program is = 2.14

The float y in this program is = 5.6

The float z in this program is = 9.37

The integer p in this program is = 81

The integer q in this program is = 44

The integer r in this program is = 29


Frequently Asked Questions

Q1

Is there any difference between the null pointer and the void pointer in C?

The null pointer is basically used in a program to assign the value 0 to a pointer variable of any data type. The void pointer, on the other hand, has no value assigned to it and we use it to store the addresses of other variables in the program- irrespective of their data types.

Q2

Is there any disadvantage to using the void pointer in C?

The void pointer is pretty viable and safe to use in a code, and it makes things very easy to manage. But, its polymorphism due to the void * can be unsafe. Once we cast any pointer to the void*, we cannot prevent it from being cast to the wrong pointer by fault, in case there is an error in the program.

Q3

What is the difference between a general pointer and a void pointer in C?

They are both the same. The void pointer in C is a pointer that is not associated with any data types. It points to some data location in the storage. This means it points to the address of variables. It is also called the general purpose pointer. In C, malloc() and calloc() functions return void * or generic pointers.

Q4

Why do we use a void pointer in C programs?

We use the void pointers to overcome the issue of assigning separate values to different data types in a program. The pointer to void can be used in generic functions in C because it is capable of pointing to any data type. One can assign the void pointer with any data type’s address, and then assign the void pointer to any pointer without even performing some sort of explicit typecasting. So, it reduces complications in a code.

Keep learning and stay tuned to get the latest updates on GATE Exam along with GATE Eligibility CriteriaGATE 2023GATE Admit CardGATE Syllabus for CSE (Computer Science Engineering)GATE CSE NotesGATE CSE Question Paper, and more.

Also Explore,

Comments

Leave a Comment

Your Mobile number and Email id will not be published.

*

*