man*_*ane 1 c pointers void-pointers
可能重复:
取消引用void指针
我有这样一个函数调用:
void foo(void *context) //function prototype
..
..
..
main()
{
.
.
foo(&(ptr->block)); //where ptr->block is of type integer.
.
.
.
void foo(void *context)
{
Here I try to use the ptr->block but am having problems. I have tried
if((int *)context ==1)
..
..
}
Run Code Online (Sandbox Code Playgroud)
我在函数中将它转换回int以使用它.我在foo()函数中取消引用它是错误的吗?
if((int *)context ==1)
Run Code Online (Sandbox Code Playgroud)
你想要这个:
if(*(int *)context ==1)
Run Code Online (Sandbox Code Playgroud)
你正在转向一个指针,int但你真正想要的是实际访问它,int所以你需要取消引用指针.