in Technology by
What ais the use of void pointers?

1 Answer

0 votes
by

A very important feature of the void pointer is reusability. Using the void pointer we can store the address of any object and whenever required we can get back the object through the indirection operator with proper casting.

Let see an example code,

#include <stdio.h>
int main()
{
void *pvData;
int iData = 10;
char cData = 'A';
float fData = 27.6;
//Assigning address of character
pvData = &cData;
//dereferencing void pointer with character typecasting
printf("cData = %c\n\n",*((char*)pvData));
//Assigning address of integer
pvData = &iData;
//dereferencing void pointer with integer typecasting
printf("iData = %d\n\n",*((int *)pvData));
//Assigning address of float
pvData = &fData;
//dereferencing void pointer with float typecasting
printf("fData = %f\n\n",*((float *)pvData));
return 0;
}

Output:

void pointer

Explanation: In the above code, pvData is a void pointer. Using it I am storing the address of the different variables (float, int, and char) and after that getting back their values using the indirection operator and proper typecasting.

You can see in the example code, how a single pointer is dealing with different types of variables. This is a very interesting feature of the void pointer that makes the programmer helpless to use the void pointer.

Related questions

0 votes
    What is void or Generic pointers in C?...
asked Jan 21, 2021 in Technology by JackTerrance
0 votes
    What is the advantage of declaring void pointers?...
asked Jan 17, 2021 in Technology by JackTerrance
+1 vote
    What is the advantage of declaring void pointers in C-Programming?...
asked Nov 9, 2020 in Technology by JackTerrance
0 votes
    When should we use pointers in a C program?...
asked Jan 22, 2021 in Technology by JackTerrance
0 votes
    Is there a side effect in doing this: C code: struct foo { int k; }; int ret_foo(const struct ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 13, 2022 in Education by JackTerrance
0 votes
    What are the use of front and rear pointers in CircularQueue implementation? (a) Front pointer points to first ... Framework of Java Select the correct answer from above options...
asked Mar 1, 2022 in Education by JackTerrance
0 votes
    What are the use of front and rear pointers in CircularQueue implementation? (a) Front pointer points to ... questions and answers pdf, java interview questions for beginners...
asked Oct 25, 2021 in Education by JackTerrance
0 votes
    What is the advantage of a void pointer in C?...
asked Jan 22, 2021 in Technology by JackTerrance
0 votes
    Can math operations be performed on a void pointer?...
asked Jan 22, 2021 in Technology by JackTerrance
0 votes
    What is the size of a void pointer in C?...
asked Jan 21, 2021 in Technology by JackTerrance
0 votes
0 votes
    int find(int i) { if(>1) j=find(j/10)+(j%10) else j=0; return j; void main() int i=1000 int k k=find(i): printf(“%d”, K) Select the correct answer from above options...
asked Dec 29, 2021 in Education by JackTerrance
0 votes
    int find(int j) { if(>1X j=find(j/ 10)+(%10); } else { j=0; return j; void main() int i=1000, int k; k=find(i); printf(“%d”, k); Select the correct answer from above options...
asked Dec 29, 2021 in Education by JackTerrance
0 votes
    class Modulus { public static void main(String args[]) { double a = 25.64; int b = 25; a = a % 10; b = b % 10; ... (a + + b); } } Select the correct answer from above options...
asked Dec 19, 2021 in Education by JackTerrance
...