c - 直接从指针使用结构体

标签 c pointers

我有一个奇怪的问题。例如,如果我知道在某个位置

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/

相关文章:

c - 如何记住缓冲区中的多个索引以便稍后访问它们以进行一一修改...记住优化

c - 在另一个结构中查找结构变量的偏移量时,左值需要作为一元 '&' 操作数

c - 如何在c中比较两个无符号整数(uint8_t)

c - 运行时错误 "Segmentation Fault"。如何去除它? (我在 Ubuntu 中工作。)

c - 将指针 str 分解为更小的指针 str

c - 如何将值传递给 Make,该值将传递给 C 代码

char * 变量地址 vs. char [] 变量地址

C++ 指针有帮助吗?

c - OpenMP Monte_Carlo 模拟实现目标与 PI 的接近度

c++ - 在 C++ 文件中使用仅 C header 实现时 undefined symbol