我有一个奇怪的问题。例如,如果我知道在某个位置
0x10000005
是我制作的一个结构。我有一个指针指向
0x10000000
如果我使用指针操作并使其指向 0x10000005,如果我知道那里有什么结构,我可以使用 0x10000005 处的内容吗?
最佳答案
If I use pointer manipulation
这是一个粗略的陈述。在 32 位 x86 系统上,指针为 4 个字节。在 64 位 x86 系统上,指针为 8 个字节。当然,我愿意打赌存在一个指针为 5 个字节的系统,但我愿意把钱花在误解上而不是晦涩的架构上。
C11 标准确实保证了这一点:
§6.7.2.1
15 Within a structure object, the non-bit-field members ... have addresses that increase in the order in which they are declared. A pointer to a structure object, suitably converted, points to its initial member ... There may be unnamed padding within a structure object, but not at its beginning.
这意味着获取对象地址的非欺骗方法是有效的:
struct s* p = &so;
关于c - 直接从指针使用结构体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32982830/