开发者

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

开发者 https://www.devze.com 2022-12-18 15:48 出处:网络
void main() { void *v; int integer=2; int *i=&integer; v=i; printf(\"开发者_开发问答%d\",(int*)*v);
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?


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


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.


Change:

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

to this:

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

精彩评论

暂无评论...
验证码 换一张
取 消