views:

70

answers:

3
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?

+3  A: 

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.

Kyle Lutz
just something to add.the compiler doesn't know what are "void" type so it dont know hot to retrieve them (as oppose to compiler expecting 4 bytes for integer, 8 bytes for double and etc), hence you will get the error.
YeenFei
+2  A: 

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 *.

AndreyT
thanks for a clear answer:)
Vijay Sarathi
Try "*(int*)v", that will solve your specific problem.
Sam Post
i know this will solve but i was more interested in knowing about the error...anyways thanks for ur comment.
Vijay Sarathi
+1  A: 

Change:

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

to this:

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