How to use maloc with strcut

M

Mateusz_madi

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'
??
 
S

Seebs

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
 
M

Mateusz_madi

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

Thank's seebs
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top