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

Discussion in 'C Programming' started by nick, Oct 16, 2005.

  1. nick

    nick Guest

    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!
     
    nick, Oct 16, 2005
    #1
    1. Advertising

  2. nick

    pete Guest

    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
     
    pete, Oct 16, 2005
    #2
    1. Advertising

  3. 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.
     
    Martin Ambuhl, Oct 16, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sander Odekerken

    Meaning of output value?

    Sander Odekerken, May 18, 2004, in forum: VHDL
    Replies:
    2
    Views:
    2,499
    John_H
    May 18, 2004
  2. Parthav
    Replies:
    4
    Views:
    1,909
    Mike Treseler
    Jan 13, 2006
  3. Frank

    Meaning of <%#

    Frank, Dec 30, 2003, in forum: ASP .Net
    Replies:
    4
    Views:
    580
  4. .NET Follower

    Meaning Of Timeout in FormsAuthentication???

    .NET Follower, Feb 6, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    368
    .NET Follower
    Feb 6, 2004
  5. Replies:
    4
    Views:
    770
Loading...

Share This Page