【发布时间】:2019-04-24 12:26:27
【问题描述】:
我正在用 C 编写一个程序,它扫描值并按照“团队 1 值 1、团队 2 值 1、团队 1 值 2、团队 2 值 2”等顺序打印出值。 我在'printf("Team 1 weights: %i\n", team1);'这一行不断收到错误消息带有消息“格式指定 int 类型,但参数的类型为“int *”,我不知道为什么。我认为这可能与数组有关。 如有任何帮助,我将不胜感激!
#include <stdio.h>
#include <math.h>
int main() {
printf("How many members of each team are there?\n");
int team1 [] = {110, 113, 112, 117};
int team2 [] = {106, 102, 121, 111};
scanf("%i", &team1[4]);
printf("How much do the players of team 1 weigh?\n");
scanf("%i", &team1[4]);
printf("Team 1 weighs: %i\n", team1);
return 0;
}
}
我希望输出打印我在请求用户输入时输入的值,例如“1队的队员有多重?”我的答案是:43、25、64、35。 打印“第 1 队体重:43、25、64、35”。
【问题讨论】:
-
[4]指的是数组的第五个元素。 -
是时候了解循环了
-
在
C数组索引从零开始。而这个int team1 [] = {110, 113, 112, 117};允许您使用从0到3的索引。没有team1[4]。导致数组元素越界是未定义的行为。 -
@P.W “第五个元素” Multipass?
标签: c