4
votes

I will just keep things simple:

Here is the function I built, which will create a new dnode:

struct dnode *dnode_create() {
        return calloc(1, sizeof(struct dnode));
    }

and here is how I call it:

struct dnode *newnode = dnode_create();

I don't understand how this involved with integer?

1
lemme guess; using visual studio? - Ed S.
Ahhh. C99 did away with implicit int, but gcc still allows it. - Ed S.
So you mean if I switch to another compiler it may wont complain about this? - Nob Wong
@LidongGuo: That is wrong in two ways. first, it doesn't address the issue here. Second, you never cast the return value of malloc in C. There is no benefit and it can actually hide an error (the error being experienced here it just so happens). You are proposing to introduce a more egregious error to silence a warning. - Ed S.

1 Answers

11
votes

Either calloc or dnode_create doesn't have a prototype in view when you try to use it.

That means it assumes an int return type, hence your warning message.

To ensure a prototype for calloc is in view, include the stdlib.h header file.

If it's the dnode_create, you have to do it yourself, by either:

  • defining the function before you use it (in a given source file); or
  • declaring a prototype before you use it.

Expanding on that, both of these will work, assuming they're sequenced this way in a single translation unit (simplistically, source file). First:

struct dnode *dnode_create (void) {         // declare & define
    return calloc(1, sizeof(struct dnode));
}
:
{   // inside some function
    struct dnode *newnode = dnode_create(); // use
}

Or:

struct dnode *dnode_create (void);          // declare
:
{   // inside some function
    struct dnode *newnode = dnode_create(); // use
}
:
struct dnode *dnode_create (void) {         // define
    return calloc(1, sizeof(struct dnode));
}

You'll notice also that I've used void in the function declaration in both cases above. There's a subtle difference between that (no parameters) and the empty parameter list (an indeterminate number of parameters). You should use the former if you really want a parameter-less function.