gpt4 book ai didi

c - Big-endian 或 Little-endian 对结构成员的影响是什么?

转载 作者:太空宇宙 更新时间:2023-11-04 07:53:57 26 4
gpt4 key购买 nike

<分区>

给出下面的代码片段:

#include <stdio.h>
#include<string.h>
union xyz
{

int j;
char b[2];
}y;


int main(void)
{

y.j=520;
printf("%d %d",y.b[0],y.b[1]);
y.b[0]=2;
printf(" %d ",y.j);
return 0;

}

对于这段代码,我不知道系统是小端还是大端,如果 y.j 是 520,那么我们有如下表示:

0000001000000100 , so y.b[0] should be 2 and y.b[1] should be 4 ,but I am getting y.b[0] as 4 and y.b[1] as 2 ,and my machine is little-endian ,but I am not getting how is the architecture affecting the representation of y.b[0] and y.b[1] ?

在我看来,每当我们创建数组时,都没有像数组的最高有效字节和数组的最低有效字节这样的东西,我们有像 b[0] 、 b[1] 、 b[2] 等等这样的索引,所以那么架构如何影响数组的表示?

请解释。

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