gpt4 book ai didi

c - C 中的数组大小和地址

转载 作者:行者123 更新时间:2023-12-02 05:23:08 25 4
gpt4 key购买 nike

当我编译下面的代码时,它显示 y 和数组的开头相隔 60 个单位。但根据我的计算,它应该是 4 * 10(对于数组)+ 4(对于 k)+ 4(对于 y)= 48。array[12] = 17 也被分配给元素 12,因为没有元素 12,实现应该转到 y 并用 17 覆盖 y。然而控制台打印 y = 10 而不是......我真的很困惑......请帮助!

 #include <stdio.h>

int main(void) {
int x = 42;
int y = 10;
int k = 10;
int array[10];

array[12] = 17;
printf("array starts at %d\n", &array[0]);
printf("x has the value %d\n", x);
printf("x is stored in location %d\n", &x);

printf("y has the value %d\n", y);
printf("y is stored in location %d\n", &y);
}

最佳答案

这称为未定义行为(写入 10 元素数组中的数组 [12]),因此根据定义您无法知道它应该做什么。 C 中没有运行时检查,所以您可以写入任何您想要的地方(好吧,为了这个例子),您不知道到底会发生什么。

关于c - C 中的数组大小和地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1481968/

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