[英]‘void*’ is not a pointer-to-object type
struct limit{
int up;
int down;
};
void *x;
struct limit *l;
l->up=1;
l->down=20;
x=l;
cout<<x->up;
This is part of my code I am getting error in last line 'void*' is not a pointer-to-object type . 这是我的代码的一部分,我在最后一行'void *'中出错, 这不是指向对象的指针类型 。 I know last line in my code is wrong. 我知道代码的最后一行是错误的。 I just want to know how to print up and down values using x
variable. 我只想知道如何使用x
变量上下打印值。
In this part: 在这一部分:
struct limit *l;
l->up=1;
l->down=20;
you are dereferencing uninitialized pointer l
, which results in undefined behavior . 您正在取消引用未初始化的指针l
,这将导致未定义的行为 。 However, even if you initialized it properly, after you assign it to void*
, you can not dereference void
pointer: 但是,即使您正确地初始化了它,将其分配给void*
,也无法取消引用void
指针:
void* x = l;
cout<< x->up;
you need to explicitly cast it back to struct limit*
: 您需要将其显式转换回struct limit*
:
void* x = l;
struct limit * y = static_cast<struct limit*>(x);
cout << y->up;
or yet even better: avoid using void*
at first place. 甚至更好:避免一开始使用void*
。
Since you mentioned that you're doing this because of pthreads , then this answer will help you :) 既然您提到您是因为pthreads而这样做的,那么此答案将对您有所帮助:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.