-1


Could anyone tell me what does this warning mean?

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


I am trying to execute the following code. The platform is GCC 4.8.1 on Windows x64(TDM-GCC). The problem is as far as I see in accessing structure members.

#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);
    }
}
mantal
  • 1,093
  • 1
  • 20
  • 38
rusty_cool
  • 27
  • 4
  • 5
    you forgot to include `.` – Jayesh Bhoi Sep 09 '14 at 11:09
  • +1 for not casting the return value of `malloc()` and therefore catching the error! – pmg Sep 09 '14 at 11:20
  • 1
    For your information, this particular issue was what created the whole [do not cast the result of malloc debate](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc). Did you compile this as C90 or C99? You might get different warnings/errors if you do. Older, crappier C90 compilers might not even give a warning, but leave the bug hidden in your program. – Lundin Sep 09 '14 at 11:29

1 Answers1

4

It means the compiler was unable to find a declaration of your malloc. Therefore it generates a default int malloc(...) declaration for you. Just include <stdlib.h>.

dragosht
  • 3,237
  • 2
  • 23
  • 32