Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

warning: initialization makes pointer from integer without a cast

Tags:

c

pointers

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?

like image 752
Nob Wong Avatar asked Dec 21 '22 00:12

Nob Wong


1 Answers

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.

like image 88
paxdiablo Avatar answered Dec 24 '22 00:12

paxdiablo