gcc的这个警告是什么意思?

时间:2014-09-09 11:07:52

标签: c pointers gcc struct malloc


谁能告诉我这个警告是什么意思?

s_sort.c: In function 'main':
s_sort.c:15:15: warning: incompatible implicit declaration of built-in function
'malloc' [enabled by default]
A[i].name = malloc(MAXCHAR*sizeof(char));


我正在尝试执行以下代码。该平台是Windows x64(TDM-GCC)上的GCC 4.8.1。 就访问结构成员而言,问题就是我所看到的。

#include <stdio.h>
#define MAX 3
#define MAXCHAR 100

int main(){
    struct STUDENT
    {
        int studentID;
        char* name;
        char grade;
    } A[MAX];
    int i;

    printf("\n");
    for (i=0;i<MAX;i++)
    {
        A[i].name = malloc(MAXCHAR*sizeof(char));
    }
    for (i=0;i<MAX;i++)
    {
        scanf("%d",&(A[i].studentID));
        scanf("%s",A[i].name);
        scanf("%c",&(A[i].grade));
    }
    printf("\n");
    for (i=0;i<MAX;i++)
    {
        printf("%d ",A[i].studentID);
        printf("%s ",A[i].name);
        printf("%c ",A[i].grade);
        printf("\n");
    }
    for (i=0;i<MAX;i++)
    {
        free(A[i].name);
    }
}

1 个答案:

答案 0 :(得分:4)

这意味着编译器无法找到malloc的声明。因此,它会为您生成默认的int malloc(...)声明。只需include <stdlib.h>