gpt4 book ai didi

c - 数组和 scanf 问题;与 scanf 一起使用的值

转载 作者:太空宇宙 更新时间:2023-11-04 07:06:51 30 4
gpt4 key购买 nike

这是一个计算房间内人员年龄的简单程序。我正处于初始阶段,现在我看到我不知道哪些变量(我的意思是我在 scanf 之前声明的变量,然后是 scanf 中的占位符)用于 scanf;如何选择和应用正确的变量。是否有可以用简单的英语解释这些问题的资源?这是程序:

// Ages people by a year. Arrays

#include <stdio.h>

int main (void)
{
// determine number of people
int n;
do
{
printf("Number of people in room: ");
scanf ("%i", &n);
}
while (n<1); // get the number of people in the room, pass through user
// again and again until the user gives a positive integer

// declare array in which to store everyone's age

int ages[n];
int i;


for (i = 0; i < n; i++)
{
printf("Age of person #%i: ", i + 1); // person number 1, person number 2, etc
scanf ("%d", ages[i]); // store the age in the i-th part of the array ages
}

// report everyone's age a year hence
printf("Time passes...\n\n");

for (i = 0; i < n; i++)
{
printf(" A year from now person #%i will be %i years old.\n", i + 1, ages[i] + 1);
// we add 1 year to previous age

}
}

最佳答案

scanf("%d")期望一个地址作为参数。因此,替换

scanf ("%d", ages[i]);

scanf ("%d", ages + i);

(或 &ages[i] 但这是个人喜好。)

关于c - 数组和 scanf 问题;与 scanf 一起使用的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32295037/

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