我可以对空指针使用指针算术吗? [英] Can I use pointer arithmetic on pointers to void pointers?
问题描述
我知道您不能在void指针上使用指针算术,但是理论上您可以在指向void指针的指针上进行指针算术,因为sizeof(void *)
会得出一个指针在系统上占用多少字节的答案?>
I know you can't utilize pointer arithmetic on void pointers, but could you theoretically do pointer arithmetic on pointers to void pointers, since sizeof(void *)
would yield an answer of how many bytes a pointer takes on your system?
推荐答案
在void*
上不允许使用指针算术,因为void
是不完整的对象类型.
Pointer arithmetic is not permitted on void*
because void
is an incomplete object type.
来自C委员会N1570草案:
From C Committee draft N1570:
6.5.6加法运算符
...
2.另外,两个操作数都应具有算术类型,或者一个操作数应是指向完整对象类型的指针,而另一个操作数应具有整数类型.
6.5.6 Additive operators
...
2. For addition, either both operands shall have arithmetic type, or one operand shall be a pointer to a complete object type and the other shall have integer type.
但是在void**
上允许使用,因为void*
不是不完整的对象类型.就像一个指向字符类型的指针.
But it is permitted on void**
because void*
is NOT an incomplete object type. It is like a pointer to a character type.
6.2.5类型
...
19. 无效类型包含一组空值; 这是一个不完整的对象类型,无法完成.
...
28. 指向void的指针应具有与相同的表示和对齐要求 指向字符类型的指针.
6.2.5 Types
...
19. The void type comprises an empty set of values; it is an incomplete object type that cannot be completed.
...
28. A pointer to void shall have the same representation and alignment requirements as a pointer to a character type.
这篇关于我可以对空指针使用指针算术吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!