学习 C 中的数组

Learning arrays in C

我正在尝试制作一个允许用户插入 5 个数字的程序,定义它们是偶数还是奇数,将它们存储到数组并打印用户插入到数组中的数字。

这是一个非常简单的程序,我正在做它只是为了学习如何使用数组。问题是它没有打印出数组的值。为什么?

int main () {
int list [5];
int i = 0;

while (i < 5) {
    printf("Insert the number\n");
    scanf("%d", &list[i]);
    
    if (i % 2 == 0) {
        printf("Even\n");
    }
    else{
        printf("Odd\n");
    }
    i++;
}

i=0;
while (i < 5){
    printf("%d\n", &list[i]);
    i++;
}
return 0;
}

您在函数 printf 的调用中使用了不正确的参数

printf("%d\n", &list[i]);
               ^^^^^^^^

你必须写

printf("%d\n", list[i]);
               ^^^^^^^

也就是说当你使用函数时scanf你必须通过引用传递参数

scanf("%d", &list[i]);

在 C 中,按引用传递意味着通过指向参数的指针间接传递参数。

当您使用函数 printf 时,按值传递参数就足够了,因为它是输出的值。

不要在您的 printf 语句中使用运算符的地址。

printf("%d\n", &list[i]);

应该是:

printf("%d\n", list[i]);