what is the meaning of "implicit declaration of function `malloc'"?

N

nick

the following is my programming code and compile message
why the warning message arise, have i done somethings wrong?


#include<stdio.h>
typedef struct card{

int abc;
}card;

int main(){


card *d;
d = (card*) malloc(sizeof(card));


return 0;

}

the compile message :

--------------------Configuration: test - Debug--------------------
Compiling source file(s)...
test.c
test.c: In function `main':
test.c:11: warning: implicit declaration of function `malloc'
Linking...

test.exe - 0 error(s), 1 warning(s)

thanks!
 
P

pete

nick said:
the following is my programming code and compile message
why the warning message arise, have i done somethings wrong?

#include<stdio.h>

You neglected to write this here:

#include said:
d = (card*) malloc(sizeof(card));
test.c:11: warning: implicit declaration of function `malloc'

The declaration for malloc, is in stdlib.h.
Without the declaration in scope,
C89 assumes malloc returns type int.

With the declaration in scope,
the compiler knows that malloc returns type pointer to void.
The (card*) cast that you used, suppressed the warning
that you would have gotten for assigning an int value
to a pointer type.

Add
#include <stdlib.h>
and lose the cast.
 
M

Martin Ambuhl

nick said:
the following is my programming code and compile message
why the warning message arise, have i done somethings wrong?

The error message
test.c:11: warning: implicit declaration of function `malloc'
tells you: you have failed to have a declaration for the function
'malloc' in scope. This is easily fixed with
#include <stdlib.h>
No doubt you failure to #include said:
#include<stdio.h>
typedef struct card{
int abc;
}card;

int main(){
card *d;
d = (card*) malloc(sizeof(card));

There is no excuse for the cast. If you put it in to stop complaints
from the compiler, you treated the symptom rather than the problem.
The normal form of this call is
d = malloc(sizeof *d);
And you should check the returned value.
> return 0;
> }

These and similary issues are covered in the FAQ. Before posting to a
newsgroup, check its FAQ and follow the newsgroup for several weeks (The
google.groups.com archive makes this 'following for several weeks'
something that you can do in a much shorter time). Following these two
simple rules will keep you from asking old, tired questions that have
been answered repeatedly. Consider that your question has been asked
hundreds of times. There are two results: very few people will bother
to answer yet again the same question to which you *should* have already
found the answer by checking the FAQ, and people become angry that you
presume that we should waste our time answering a question that you
won't bother putting in any effort to find the answer on your own.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top