Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > what is the meaning of "implicit declaration of function `malloc'"?

Reply
Thread Tools

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

 
 
nick
Guest
Posts: n/a
 
      10-16-2005
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!
 
Reply With Quote
 
 
 
 
pete
Guest
Posts: n/a
 
      10-16-2005
nick wrote:
>
> 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 <stdlib.h>

> 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.

--
pete
 
Reply With Quote
 
 
 
 
Martin Ambuhl
Guest
Posts: n/a
 
      10-16-2005
nick wrote:
> 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 <stdlib.h> accounts for the silly cast
on the return value from malloc()
>
>
> #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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
type declaration in declaration of a parameter or return type of a function Luca Forlizzi C Programming 4 11-14-2010 09:30 PM
Understanding search queries, semantics, and "Meaning" ...aren't weall looking for meaning? 5lvqbwl02@sneakemail.com Python 4 01-14-2009 02:28 PM
Can a static function declaration conflict with a non-static declaration? nospam_timur@tabi.org C Programming 4 12-12-2006 10:26 PM
Variable declaration taken as a function pointer declaration Bolin C++ 4 12-02-2005 05:28 PM
Function declaration in class declaration Ovidesvideo C++ 4 12-10-2004 06:36 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57