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

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

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

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:
    8,001
  2. M K
    Replies:
    11
    Views:
    7,715
    woods_jemmy
    Apr 9, 2008
  3. Simon
    Replies:
    1
    Views:
    664
    Henri
    Nov 9, 2004
  4. =?Utf-8?B?S2hhaXJvbA==?=
    Replies:
    0
    Views:
    477
    =?Utf-8?B?S2hhaXJvbA==?=
    Dec 10, 2004
  5. =?Utf-8?B?SmVmZiBLZXNpbmdlcg==?=

    Common Web functions dll (Used to use MTS, what do I use in .NET

    =?Utf-8?B?SmVmZiBLZXNpbmdlcg==?=, Dec 28, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    677
    charlie
    Apr 30, 2005
  6. =?Utf-8?B?ZGF2aWQ=?=
    Replies:
    1
    Views:
    4,026
    Ken Cox [Microsoft MVP]
    Apr 19, 2005
  7. =?Utf-8?B?Q3JhaWc=?=

    when to use usercontrol, when not to use?

    =?Utf-8?B?Q3JhaWc=?=, Jun 15, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    482
    Aaron Corcoran
    Jun 15, 2005
  8. Strcut padding?

    , Feb 23, 2008, in forum: C++
    Replies:
    1
    Views:
    474
    EventHelix.com
    Feb 27, 2008
Loading...