unsigned int addr = 0x1000;
int temp = *((volatile int *) addr + 3);
它是否将递增的指针(即 addr + 3 * sizeof(int)
)视为指向 volatile int
的指针(同时解除引用)。换句话说,我可以期待 temp 中 say (0x1012
) 的硬件更新内容吗?
最佳答案
是的。
指针算法不影响指针的类型,包括任何类型限定符。给定 A + B
形式的表达式,如果 A
具有指向 T 的类型限定指针并且 B
是整数类型,则表达式 A + B
也将是指向 T 的限定指针——相同的类型,相同的限定符。
来自 C 规范的 6.5.6.8(草案 n1570):
When an expression that has integer type is added to or subtracted from a pointer, the result has the type of the pointer operand.
关于c - 递增后取消引用指向 volatile int 的指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36636432/