How to use maloc with strcut

Discussion in 'C Programming' started by Mateusz_madi, May 13, 2010.

  1. Mateusz_madi

    Mateusz_madi Guest

    I have program:
    --------------
    #include<stdio.h>
    struct s{
    int x;
    struct s* nxt;
    };

    void f(struct s* pt)
    {
    malloc(sizeof(struct s));
    }
    .....
    Why i get warrning when i'am trying to compile it sth like:
    Incompatible implicit declaration of build-in function 'malloc'
    ??
    Mateusz_madi, May 13, 2010
    #1
    1. Advertising

  2. Mateusz_madi

    Seebs Guest

    On 2010-05-13, Mateusz_madi <> wrote:
    > I have program:
    > --------------
    > #include<stdio.h>
    > struct s{
    > int x;
    > struct s* nxt;
    > };
    >
    > void f(struct s* pt)
    > {
    > malloc(sizeof(struct s));
    > }
    > ....
    > Why i get warrning when i'am trying to compile it sth like:
    > Incompatible implicit declaration of build-in function 'malloc'
    > ??


    This has nothing to do with the struct, and everything to do with the fact
    that you called malloc(), but you never included <stdlib.h>, so it hasn't
    been declared. The compiler knows about malloc, though.

    In C89, if you used a function without a declaration, it was assumed to return
    int. malloc() doesn't return int, and the compiler is warning you that
    this probably means something has gone wrong. Which it has.

    -s
    --
    Copyright 2010, all wrongs reversed. Peter Seebach /
    http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
    http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
    Seebs, May 13, 2010
    #2
    1. Advertising

  3. Mateusz_madi

    Mateusz_madi Guest

    On 13 Maj, 19:39, Seebs <> wrote:
    > On 2010-05-13, Mateusz_madi <> wrote:
    >
    >
    >
    > > I have program:
    > > --------------
    > > #include<stdio.h>
    > > struct s{
    > > int x;
    > > struct s* nxt;
    > > };

    >
    > > void f(struct s* pt)
    > > {
    > >  malloc(sizeof(struct s));
    > > }
    > > ....
    > > Why i get warrning when i'am trying to compile it sth like:
    > > Incompatible implicit declaration of build-in function 'malloc'
    > > ??

    >
    > This has nothing to do with the struct, and everything to do with the fact
    > that you called malloc(), but you never included <stdlib.h>, so it hasn't
    > been declared.  The compiler knows about malloc, though.
    >
    > In C89, if you used a function without a declaration, it was assumed to return
    > int.  malloc() doesn't return int, and the compiler is warning you that
    > this probably means something has gone wrong.  Which it has.
    >
    > -s
    > --
    > Copyright 2010, all wrongs reversed.  Peter Seebach / ://www.seebs.net/log/<-- lawsuits, religion, and funny pictureshttp://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!


    Thank's seebs
    Mateusz_madi, May 13, 2010
    #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. Replies:
    6
    Views:
    7,089
  2. M K
    Replies:
    11
    Views:
    7,024
    woods_jemmy
    Apr 9, 2008
  3. Simon
    Replies:
    1
    Views:
    435
    Henri
    Nov 9, 2004
  4. Strcut padding?

    , Feb 23, 2008, in forum: C++
    Replies:
    1
    Views:
    391
    EventHelix.com
    Feb 27, 2008
  5. Replies:
    0
    Views:
    434
Loading...

Share This Page