我对 C 语言编程还比较陌生。我正在尝试为学校项目编写学校记录管理系统。我理解您关于“我不会为您做作业”的政策,因此我不会询问如何做所有事情,但我会提供我正在处理的完整代码。我目前面临的问题是,我使用 strcpy 和 strcmp 来防止用户将已经存在的 ID 分配给新学生,但编译器显示了一个我不太明白的警告/问题。
这是编译器日志:
Compiler: TDM-GCC 4.7.1 64-bit Release
Executing gcc.exe...
gcc.exe "C:\Users\Sheng\Desktop\Restarting Project\ToStudent\Updated_StudentManagement System.c"
-o "C:\Users\Sheng\Desktop\Restarting Project\ToStudent\Updated_Student Management System.exe"
-I"C:\Program Files (x86)\Dev-Cpp\MinGW64\x86_64-w64-mingw32\include"
-L"C:\Program Files (x86)\Dev-Cpp\MinGW64\x86_64-w64-mingw32\lib"
-static-libgcc C:\Users\Sheng\Desktop\Restarting Project\ToStudent\Updated_StudentManagement System.c:
In function 'add_student':
C:\Users\Sheng\Desktop\Restarting Project\ToStudent\Updated_Student Management
**System.c:222:4: warning: passing argument 1 of 'strcpy' makes pointer from integer without a cast [enabled by default]**
In file included from C:\Users\Sheng\Desktop\Restarting Project\ToStudent\Updated_Student Management System.c:8:0:
**c:\program files (x86)\dev-cpp\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.7.1/../../../../x86_64-w64-mingw32/include/string.h:51:18: note: expected 'char * __restrict__' but argument is of type 'char'**
C:\Users\Sheng\Desktop\Restarting Project\ToStudent\Updated_Student Management
**System.c:222:4: warning: passing argument 2 of 'strcpy' from incompatible pointer type [enabled by default]**
In file included from C:\Users\Sheng\Desktop\Restarting Project\ToStudent\Updated_Student Management System.c:8:0:
c:\program files (x86)\dev-cpp\mingw64\bin\../lib/gcc/x86_64-w64-**mingw32/4.7.1/../../../../x86_64-w64-mingw32/include/string.h:51:18: note: expected 'const char * __restrict__' but argument is of type 'char (*)[50]'**
C:\Users\Sheng\Desktop\Restarting Project\ToStudent\Updated_Student Management
**System.c:227:5: warning: passing argument 1 of 'strcmp' makes pointer from integer without a cast [enabled by default]**
In file included from C:\Users\Sheng\Desktop\Restarting Project\ToStudent\Updated_Student Management System.c:8:0:
**c:\program files (x86)\dev-cpp\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.7.1/../../../../x86_64-w64-mingw32/include/string.h:53:15: note: expected 'const char *' but argument is of type 'char'
C:\Users\Sheng\Desktop\Restarting Project\ToStudent\Updated_Student Management System.c:227:5: warning: passing argument 2 of 'strcmp' from incompatible pointer type [enabled by default]**
In file included from C:\Users\Sheng\Desktop\Restarting Project\ToStudent\Updated_Student Management System.c:8:0:
**c:\program files (x86)\dev-cpp\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.7.1/../../../../x86_64-w64-mingw32/include/string.h:53:15: note: expected 'const char *' but argument is of type 'char (*)[50]'**
Execution terminated
Compilation successful
代码:
struct Student
{
int num_student; // number of students
char student_name[50][50]; // name of students
char student_id[50][50]; // Student ID
int student_course_num[10]; // Number of course each student enroll
int student_course[20][20]; // The course code of the student
};
void add_student()
{
FILE *filePointer;
int i;
int user_input;
int j;
int replay = FALSE;
char testID[50];
struct Student profile[20];
printf("---------------------------------------------------\n");
printf("Option 3: Adding a Student\n");
printf("---------------------------------------------------\n");
printf("\n");
filePointer = fopen("Student.txt","a+");
if(filePointer == NULL)
{
printf("\nSystem Error...");
printf("\nPress any key to exit");
getch();
}
else
{
printf("Enter the number of students to be added: ");
scanf("%d",&user_input);
for(i=0;i<user_input;i++)
{
if(TRUE && replay == FALSE)
{
printf("Enter Student Name: ");
clearBuffer();
scanf("%[^\n]",profile[i].student_name);
}
if(TRUE || replay == TRUE)
{
printf("Enter Student ID (8 digits): ");
clearBuffer();
scanf("%[^\n]",profile[i].student_id);
}
strcpy(testID[i],profile[i].student_id);
for(j=0;j<i;j++)
{
if(strcmp(testID[j],profile[i].student_id) == 0)
{
printf("The ID already exists");
i--;
replay = TRUE;
}
}
if(replay == FALSE)
{
fprintf(filePointer,"%s\n%s\n",profile[i].student_name,profile[i].student_id);
}
}
}
fclose(filePointer);
return_menu();
}
您的问题:testID 是一个
char[50]
,因此 testID[i]
是单个字符(这是一个小整数)。 strcpy()
想要一个指针作为第一个参数。所以你从指针创建一个整数(不进行转换)。
如果你想拥有许多具有固定最大大小的 testID,你的声明应该类似于
char testID[50][100];
请尝试#define
这些值的一些常量!
这些警告是因为
strcmp() or strcpy()
的第二个参数必须是空终止的 char array
(字符串)或字符指针。
您向其传递一个
char **
,即一个双精度数组,因为 Student_id 是一个数组数组。
检查这些: