如何将void指针转换为int?
我有一个void* pointer. 我想将该指针处的值转换为 int 并将其添加到 another 处的值void* x。我已经尝试过:
x += (int)(*pointer);
但这给了我一个operand of type 'void' where arithmetic or pointer type is required错误
回答
您不能取消引用指向 void 的指针。这将返回一个void不存在的 type 值。
您必须将指针强制转换为int*,然后取消引用它。
x += *(int*)pointer;