gpt4 book ai didi

C指针/数组值混淆

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

假设我们有以下代码:

typedef union {
float e[4];
__v4sf v;
float *s;
} __vec4f;

float test[12];


int main(){
__vec4f one;

printf("adrs: &one.s = 0x%x, &one.e = 0x%x\n", &one.s, &one.e);
printf("vals: one.s = 0x%x, one.e = 0x%x\n",one.s,one.e);
one.s = test;
printf("adrs: &one.s = 0x%x, &one.e = 0x%x\n", &one.s, &one.e);
printf("vals: one.s = 0x%x, one.e = 0x%x\n",one.s,one.e);

return 0;
}

运行时结果如下:

adrs: &one.s = 0xbffff270, &one.e = 0xbffff270
vals: one.s = 0x927ff590, one.e = 0xbffff270
adrs: &one.s = 0xbffff270, &one.e = 0xbffff270
vals: one.s = 0x52a0, one.e = 0xbffff270

可以看到 one.sone.e 的地址是相等的 - 正如预期的那样 - 但值不同。这就是让我感到困惑的地方。

最佳答案

one.e是one.e数组的地址。 one.s 是 one.s 的值。如果你想要 e 的值,你需要访问 one.e[0]...one.e[3]。

关于C指针/数组值混淆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9197765/

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