gpt4 book ai didi

c++ - 相同代码的C输出不同

转载 作者:行者123 更新时间:2023-11-30 21:49:04 27 4
gpt4 key购买 nike

#include <stdio.h>
#include <stdlib.h>

int main() {

int* a[10];
int* p = a;
int i = 0;
for (p = &a[0], i = 0; p < &a[10]; p++, i++)
{
*p = i;
}
for (int i = 0; i < 10; i++)
{
printf("%d\n", a[i]);
}
}

使用 eclipse 在 GCC 上的输出:

0
2
4
6
8
10
12
14
16
18

使用 Visual Studio 的输出:

0
1
2
3
4
5
6
7
8
9

为什么?

最佳答案

% gcc error.c -Wall -Wextra
error.c: In function ‘main’:
error.c:7:14: warning: initialization from incompatible pointer type [-Wincompatible-pointer-types]
int* p = a;
^
error.c:9:12: warning: assignment from incompatible pointer type [-Wincompatible-pointer-types]
for (p = &a[0], i = 0; p < &a[10]; p++, i++)
^
error.c:9:30: warning: comparison of distinct pointer types lacks a cast
for (p = &a[0], i = 0; p < &a[10]; p++, i++)
^
error.c:15:18: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘int *’ [-Wformat=]
printf("%d\n", a[i]);
^

提示:

int a[10]; // notice the lack of star here.

关于c++ - 相同代码的C输出不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45794696/

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