[英]How to cast a void pointer to an int?
I have a void* pointer
.我有一个
void* pointer
。 I want to cast the value at that pointer to an int and add it the value at another void* x
.我想将该指针处的值转换为 int 并将其添加到另一个
void* x
处的值。 I have already tried:我已经尝试过:
x += (int)(*pointer);
But this gives me a operand of type 'void' where arithmetic or pointer type is required
error但这给了我一个
operand of type 'void' where arithmetic or pointer type is required
错误
You can't dereference a pointer to void.您不能取消引用指向 void 的指针。 That would return a value of type
void
, which doesn't exist.这将返回一个不存在的
void
类型的值。
You have to cast the pointer to an int*
, then dereference that.您必须将指针转换为
int*
,然后取消引用它。
x += *(int*)pointer;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.