gpt4 book ai didi

c - union 中变量的对齐

转载 作者:太空宇宙 更新时间:2023-11-04 02:15:16 25 4
gpt4 key购买 nike

<分区>

Possible Duplicate:
A question about union in C

假设以下代码:

#include <stdio.h>

int main()
{
union a
{
int i;
char ch[2];
};
union a u;
u.ch[0]=3;
u.ch[1]=2;
printf("%d, %d, %d\n", u.ch[0], u.ch[1], u.i);
return 0;
}

我想知道为什么 ch[0]ch[1] 在 union 的低位地址上。在堆栈中,如果我有一个 Little Endian Byte,它们应该在更高阶的地址上。谁能解释 union 的内存表示?

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com