Void pointer in c

Void pointer in c

so in the previous session of pointer we understand that a pointer should be assigned an address of the same type. for example if we have int type pointer, Then we can not store the address of float variable to this int type pointer. It is incorrect. These types mixed conversion can be perform using the explicitly casts but here it lead to undefined behaviour. there is exception to this rule is a pointer to void. A pointer to void is the generic pointer that can point to any data type. so we can stored or assign the address of any data type to void pointer.

The syntax of declaration of void pointer

void *pointer name;

Declaration of the void pointer is given below:

void *ptr;

In this example void is a keyword and ptr is variable declared as pointer of void type. let us take examples to understand

int i=8, *ip=&i;
double d;
float f=7.9,*fp=&f;
void *ptr;
ip=fp; /* Incorrect */
ptr=ip; /* correct */
ptr=&d; /* correct */
ptr=&fp; /* correct */

Note A void pointer can not be dereferenced simply by follow or using indirectional operator. before use dereferencing it should be type cast to an appropriate pointer data type. for example if we have vp is void pointer as in the above example, and it hold the address of integer data type variable (integer variable), Then we can not dereference it by writing *vp, we will have to writev*(int *)vp, here left most asterisk is indirection operator and (int *) is use for typecasting.

/* proramme of dereferencing a void pointer */
#include<stdio.h>
int
main()
{
int x=6;
float y=8.8,*fp=&y;
void *ptr;
ptr=&x;
printf("value of x is %d",*(int *)ptr);
*(int *)ptr=89;
printf("value of x= %d\n",*(int *) ptr);
ptr=fp;
printf("value of y = %f\n",*(float *)ptr);
return 0;
}

Size of the void pointer in C

The size of the void pointer in C language equals to the pointer of character type and the representation of a pointer to void is the same as the pointer of character type. And The size of the pointer will will depending on the platform that you are using

Let’s Take the example:

/* program for size of void pointer */
#include<stdio.h>
int main()
{
void *vptr = NULL; //void type pointer
int *p = NULL;// integer type pointer
char *cp = NULL;//character type pointer
float *fp = NULL;//float type pointer
//size of the void pointer
printf("size of void pointer = %d\n\n",sizeof(vptr));
//size of character pointer
printf("size of character pointer = %d\n\n",sizeof(cp));
//size of integer pointer
printf("size of integer pointer = %d\n\n",sizeof(p));
//size of float pointer
printf("size of float pointer = %d\n\n",sizeof(fp));
return 0;
}

size of void pointer = 8
size of character pointer = 8
size of integer pointer = 8
size of float pointer = 8

Advantages of void pointer

1. The calloc() and malloc() function return the void pointer, so these functions can be used to allocate the memory of any data type because void support all type.

2. And the void pointer in C langauge can also be used to implement the generic functions