gpt4 book ai didi

有人能解释一下为什么这个程序会这样吗?

转载 作者:行者123 更新时间:2023-11-30 18:16:59 25 4
gpt4 key购买 nike

我在 C 中有以下程序:

void main() {
int x[3];
int* p = &x;
printf("%d %d \n", p, x);
}

p 和 x 的值似乎是相同的,但我不明白为什么,因为 p 是指针 x 的地址,不应该有什么东西就像 *p = x 而不是 p = x

最佳答案

您的代码在编译时会产生一些警告:

warning: initialization from incompatible pointer type [-Wincompatible-pointer-types]
warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘int *’ [-Wformat=]
warning: format ‘%d’ expects argument of type ‘int’, but argument 3 has type ‘int *’ [-Wformat=]

并且您编译的代码会导致未定义的行为,并且相同的结果是由于未定义的行为造成的。

提示:您的代码在概念上是错误的,请详细阅读C语言中的指针、数组e类型。

关于有人能解释一下为什么这个程序会这样吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59893161/

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