C语言 表达式必须是使用简单指针算法的指向完整对象类型的指针
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24472724/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Expression must be a pointer to a complete object type using simple pointer arithmetic
提问by 010110110101
I'm trying to do some basic pointer arithmetic with a void *. My actual code computes an offset by using sizeof and then multiplying. Here is sample code to show an instance of the issue by itself.
我正在尝试使用 void * 进行一些基本的指针运算。我的实际代码通过使用 sizeof 然后相乘来计算偏移量。以下示例代码可单独显示问题实例。
void * p;
p = 0;
p = p + 1;
I'm using the MSVC compiler in C (not C++).
我在 C(不是 C++)中使用 MSVC 编译器。
The error is:
错误是:
expression must be a pointer to a complete object type
I'm not understanding what this error is trying to say. There is no object or struct here.
我不明白这个错误想说什么。这里没有对象或结构。
回答by John Zwinck
Pointer arithmetic is always in terms of the size of the pointed-to object(s). Incrementing a char*will advance the address by one, whereas for int*it would usually be four (bytes). But voidhas unknown size, so pointer arithmetic on void*is not allowed by the standard. Cast to the appropriate type first; if you just want to manipulate the address as if it were a number then cast to char*or use intptr_t.
指针算法总是根据被指向对象的大小。增加 achar*会将地址提前一,而因为int*它通常是四(字节)。但是void大小未知,因此void*标准不允许进行指针运算。首先转换为适当的类型;如果您只想像操作数字一样操作地址,则将其强制转换为char*或使用intptr_t.

