Warning: initialization makes a pointer out of the whole without casting

I will just keep things simple:

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

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

and here is what I call him:

 struct dnode *newnode = dnode_create(); 

I do not understand how this relates to the whole?

+4
source share
1 answer

Either calloc or dnode_create does not have a prototype when you try to use it.

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

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

If it is dnode_create , you must do it yourself, either:

  • defining a function before using it (in this source file); or
  • Declaring a prototype before using it.

Expanding, both of them will work, assuming that they are ordered in this way in one translation unit (simplified, in the source file). First of all:

 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 will also notice that I used void in the function declaration in both cases above. There is a subtle difference between them (without parameters) and an empty list of parameters (an indefinite number of parameters). You should use the first if you really need a function without parameters.

+11
source

Source: https://habr.com/ru/post/1500420/


All Articles