void pointer in C

At the point when we announce a pointer we indicate its sort which will be same as the

kind of the variable whose address the pointer will contain. For instance in the event that we will

announce a whole number pointer, at that point it can contain the location of a whole number variable as it were.

Take underneath example.

int *p,a;
char b;
p=&a;  //valid
p=&b;	 //invalid, will show an error because p can contain address of integer type variable only

Imagine a scenario where we can have a pointer that can point to a variable.

This should be possible effectively utilizing void pointer.

void pointer in C

void pointer is a pointer which isn’t related with any information type. It can contain

the location of the variable of any information type. void pointer is

otherwise called broadly useful pointer. We can proclaim a void

pointer in C utilizing void catchphrase as demonstrated as follows.

void *p;
int a;
char b;
p=&a;	//valid
p=&b;	//valid

The dereference administrator or indirection administrator (*) is utilized for dereferencing a pointer.

Dereferencing means getting to the incentive at the location put away in the pointer variable. We need to type station the pointer variable to dereference

it on the grounds that the void pointer isn’t related with any information type. The compiler can’t discover

the sort of factor pointed by the void pointer. This should be possible in the following manner.

void *p;
int a=20,b;
p=&a;
b=*p;		//this will show an error
b=*((int*)p);	//type casting the pointer variable to dereference it

We can’t play out the pointer number juggling on the void pointer. Take beneath example.

void *p;
int a=20;
p=&a;
p++;	//this will show an error

Let’s make one program to comprehend the idea of the void pointer in C.

#include<stdio.h>
 
void main()
{
	int a=20;
	float b=20.5;
	char c='c';
	void *p;
	
	p=&a;
	printf("%d",*((int*)p));
	
	p=&b;
	printf("n%f",*((float*)p));
	
	p=&c;
	printf("n%c",*((char*)p));	
}

Output:

20
20.500000
c
Press any hey to continue . . . _

This is about void pointer in C. In the event that you discover any misstep or data

missing in above instructional exercise, at that point please notice in the comments.

Leave a Comment

error: Alert: Content is protected!!