小编JaJ*_*aJa的帖子

C结构内存布局偏移

我正在处理如下结构:

struct s1 {
  struct s1 *next;
  char a[64];
  char b[64];
  struct s2 *other;
};

struct s2 {
  uint32_t x;
  uint32_t y;
}
Run Code Online (Sandbox Code Playgroud)

获得指针bs1,我正在尝试使用指针算法来访问成员s1->other.我尝试过以下方法:

// Assuming we have the following
struct s1 *p1 = ...
char *b = p1->b;

// Offset to get to s2
struct s2 *p2 = (struct s2*) b + 64;

// Access members
uint32_t result = p2->x;
Run Code Online (Sandbox Code Playgroud)

我不认为填充是问题,因为我开始b.

c memory struct

0
推荐指数
1
解决办法
177
查看次数

标签 统计

c ×1

memory ×1

struct ×1