Vijay
Vijay

Reputation: 67211

compiler error in a c program:indirection on type void*

void main()
{
void *v;
int integer=2;
int *i=&integer;
v=i;
printf("%d",(int*)*v);
}

this simple program will result in a compiler error saying:

Compiler Error. We cannot apply indirection on type void*

what exact does this error mean?

Upvotes: 2

Views: 537

Answers (3)

Paul R
Paul R

Reputation: 212949

Change:

printf("%d",(int*)*v);

to this:

printf("%d",*(int*)v);

Upvotes: 1

AnT stands with Russia
AnT stands with Russia

Reputation: 320401

The error means exactly what it says. The error is triggered by the *v subexpression used in your code.

Unary operator * in C is often called indirection operator or dereference operator. In this case the compiler is telling you that it is illegal to apply unary * to a pointer of type void *.

Upvotes: 3

Kyle Lutz
Kyle Lutz

Reputation: 8036

You cannot dereference pointers to void (i.e., void *). They point to a memory location holding unknown data so the compiler doesn't know how to access/modify that memory.

Upvotes: 3

Related Questions