简体   繁体   English

如何将 void 指针转换为 int?

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM