如何将void指针转换为int?

时间:2021-02-16 20:04:32

标签: c pointers

我有一个void* pointer。我想将该指针处的值转换为 int 并将其添加到另一个 void* x 处的值。我已经试过了:

x += (int)(*pointer);

但这给了我一个 operand of type 'void' where arithmetic or pointer type is required 错误

1 个答案:

答案 0 :(得分:5)

您不能取消引用指向 void 的指针。这将返回一个不存在的 void 类型的值。

您必须将指针强制转换为 int*,然后取消引用它。

x += *(int*)pointer;