In one file I have this:
#include <stdio.h>
#include <stdlib.h>
static struct node* mynode;
struct node*
example(void)
{
mynode = malloc(sizeof(struct node));
...fill up the struct here...
return mynode;
}
The calling routine is:
#include <stdio.h>
#include <stdlib.h>
int
main(void)
{
mynode=example();
}
The node itself is defined in a defs.h file I don’t show here.
The warning I get when compiled with gcc is “assignment makes pointer from integer without a cast” in the calling routine.
Changing to mynode=(struct node*)example(); removes that warning. The routine works in any case but I don’t understand why I’m getting the warning.
In the file calling
example, the return type ofexampleis not known, so it is assumed to return an int, which you assign tostruct node *. Hence the warning.You should either declare the prototype for
examplein the calling file (typing instruct node* example(void);before the calling function) or (better) create a header file called, say, example.h, where you type in the prototype and then include the header file in the calling file (that is, typing in#include "example.h" at the top). The header file thus defines the interface of the file whereexample` is located, and the files using the functions can include the header file, thus making sure all the types match, and removing any compiler warnings.