【发布时间】:2019-03-05 18:57:39
【问题描述】:
我似乎无法将用户输入输入到 C 中的动态数组中。
#include "stdio.h"
int main(void){
int counter = 0;
int x = 1;
int i;
printf("Enter the number of teams playing in the league: \n");
scanf("%d", &i);
char teams[i];
for (counter = 0; counter < i; counter++){
char teams[counter];
printf("Enter team names: \n");
scanf("%s", teams);
}
for (counter = 0; counter < i; counter++){
char teams[counter][10];
printf(" Team %d is %s \n", x, *teams);
x++;
}
}
当我运行此代码时,我得到以下输出,
Enter the number of teams playing in the league: 2
Enter team names: Team1
Enter team names: Team2
Team 1 is Team1
Team 2 is \320\365\277\357\376
Program ended with exit code: 0
无法弄清楚我的错误。希望得到任何帮助。
谢谢!
【问题讨论】:
-
你明白
char teams[counter][10];是一个完全独立的变量定义吗? -
...
char teams[counter];也是如此,一旦退出循环,这个变量就会被销毁? -
有很多问题,太多无法解决。不过我会回答你的问题 --
teams应该是char *的数组,而不是char的数组。 -
OHhhh,我想我还是个菜鸟,不过我现在明白我的错误了。谢谢大家。