4

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?

8
  • lemme guess; using visual studio? Commented Sep 4, 2013 at 2:37
  • Ahhh. C99 did away with implicit int, but gcc still allows it. Commented Sep 4, 2013 at 2:44
  • So you mean if I switch to another compiler it may wont complain about this? Commented Sep 4, 2013 at 2:48
  • 1
    possible duplicate of "warning: initialization makes pointer from integer without a cast". But I don't think it does Commented Sep 4, 2013 at 3:59
  • 2
    @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. Commented Sep 4, 2013 at 4:18

1 Answer 1

11

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.

Sign up to request clarification or add additional context in comments.

6 Comments

I should cast calloc like (strcut dnode*)?
@Nob, no, don't cast malloc/calloc in C, it can hide certain subtle errors. Instead, make sure a prototype is defined at point of use.
I do include stdlib.h. What you mean by "declaring a prototype before you use it?"
@Nob, see the update. Given you include stdlib.h, it's probably your own function.
Sorry am still confused. Isnt that the first one is just the same one as mine? Or I have to use it within curly bracket?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.