void pointer in C
In this program, you will know the concept of void pointer and get the example code of void pointer in c programming.
What is void pointer
void pointer is a type of pointer in C/C++. It does not point to a specific data type. It can be used as a generic pointer, Which can point to any data type. It is typically used for passing pointers to functions. It is also used to allocate memory or refer to data without knowing its type.
void pointers are declared by using the keyword void as the base type.
For example:
void *ptr;
void pointers are not associated with any particular data type. It can be used to point to any other types of data in memory.
Below is an example of declaring and using a void pointer:
void pointer in c example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | #include <stdio.h> int main() { // Declare void pointer void *vp; int x = 10; float y = 12.34; // Store address of integer in void pointer vp = &x; // Typecast void pointer to integer pointer int *ip; ip = (int *)vp; // Print value stored in 'ip' printf("Integer value = %d\n", *ip); // Store address of float in void pointer vp = &y; // Typecast void pointer to float pointer float *fp; fp = (float *)vp; //Print value stored in 'fp' printf("Float value = %f\n", *fp); return 0; } |
Output
Check out our other C programming Examples