我在玩指针,我注意到一件奇怪的事情。
我有一个结构如下:
typedef struct list_element_struct {
uint32_t x;
uint32_t y;
uint32_t z;
struct list_element_struct *next;
}list_element;
Run Code Online (Sandbox Code Playgroud)
据我所知,unsigned int 的大小为 4 个字节,指针的大小为 8 个字节。这里也有 8 个字节对齐,所以这个结构的大小是 24 个字节。
我已经初始化了上述结构对象的列表,list_element els[5];并将其中的每条数据设置为 0memset(els,0,5*sizeof(list_element));
现在我正在尝试使用以下代码查看它们的内存位置:
printf("%p start location of els\n", &(els));
printf("%p start location of els->x\n", &(els->x));
printf("%p start location of els->y\n", &(els->y));
printf("%p start location of els->z\n", &(els->z));
printf("%p start location of els->next(pointer)\n", &(els->next));
printf("%p start location of els+1\n", &(els[1]));
Run Code Online (Sandbox Code Playgroud)
我打印出来的是:
0x7ffeeba4a970 start location of els
0x7ffeeba4a970 start location of …Run Code Online (Sandbox Code Playgroud)