【发布时间】:2014-11-29 21:08:49
【问题描述】:
我正在尝试将学生姓名保存在 char 数组中并打印第一个学生的姓名,但我的代码不起作用。我正在练习 C,所以我自己编写随机程序,需要帮助。我的代码生成以下错误:
format '%s' expects argument of type 'char *', but argument 2 has type 'int'
我对 C 很陌生。有人可以帮忙吗?
代码:
#include <stdio.h>
#include <string.h>
main(){
char StudentsName[100];
float StudentsGrades[100];
int NumStudents, i, j;
printf("How many students you want to process? ");
scanf("%d", &NumStudents);
if(NumStudents > 15){goto skip_lines_grades;}
for(i=0; i<NumStudents; i++){
printf("\n Write the name of the student: ");
scanf("%s", &StudentsName[i]);
}
//Prints First Student, I'M GETTING THE ERROR HERE, WHY?
printf("%s", StudentsName[0]);
goto skip_last_msg;
skip_lines_grades:;
printf("We can process 15 students for now, try again.");
skip_last_msg:;
}
【问题讨论】:
-
char是一个字符。 -
您的名称数组是 100 个字符的数组,而不是 100 个字符串。你想要
char StudentsName[100][MAX_NAME_LEN]; -
@KarolyHorvath 有什么解决办法吗?
-
警告中有什么不清楚的地方?您正在使用带有 char 的 char 指针 (%s) 的 printf 格式说明符。
-
StudentsName[0] 是一个字符,字符不是用 %s 打印的。