Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to use maloc with strcut

3 views
Skip to first unread message

Mateusz_madi

unread,
May 13, 2010, 1:24:09 PM5/13/10
to
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'
??

Seebs

unread,
May 13, 2010, 1:39:51 PM5/13/10
to

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 / usenet...@seebs.net
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!

Mateusz_madi

unread,
May 13, 2010, 1:44:51 PM5/13/10
to
On 13 Maj, 19:39, Seebs <usenet-nos...@seebs.net> wrote:

> On 2010-05-13, Mateusz_madi <madi.cz...@gmail.com> 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 / usenet-nos...@seebs.nethttp://www.seebs.net/log/<-- lawsuits, religion, and funny pictureshttp://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!

Thank's seebs

0 new messages