使用strcmp和strcpy时不了解编译器警告

时间:2013-12-06 08:20:38

标签: c debugging compiler-construction warnings

我对C语言编程比较陌生。我正在尝试为学校项目编制学校记录管理系统。我理解你关于“我不会为你做功课”的政策,所以我不会问我怎么做,尽管我会提供我正在处理的整个代码。我现在面临的问题是我正在使用strcpy和strcmp来阻止用户为新学生分配一个已经存在的ID,但编译器会显示一个我不太了解的警告/问题。 / p>

这是编译器日志

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();

}

2 个答案:

答案 0 :(得分:1)

您的问题:testID是char[50],因此testID[i]是一个字符(这是一个小整数)。 strcpy()想要第一个参数指针。所以你用指针制作一个整数(没有演员)。

如果您想拥有许多具有固定最大尺寸的testID,您的声明应该类似于char testID[50][100]; 请尝试#define这些值的一些常数!

答案 1 :(得分:0)

警告是因为strcmp() or strcpy()的第二个参数必须是空终止char array(字符串)或字符指针。

您传递的是char **,即双数组,因为student_id是一个数组数组。

检查这些:

http://www.cplusplus.com/reference/cstring/strcpy/

http://www.cplusplus.com/reference/cstring/strcmp/