2

Its quite a common question but I have not got my answer so asking it again.

I have structers defined as:

struct f_lock{
              int x;
              struct f_lock *next;
     };

Then say I have a function:

struct f_lock *new_node()
{
   struct f_lock *new_f_lock;
   .....
   return new_f_lock;
}

Which I call from another function:

struct f_lock *new_f_lock;
new_f_lock = new_node(); //This line gives the error warning:assignment makes pointer from integer without a cast

Would be grateull for help Thanks

4
  • 4
    You most likely haven't declared a prototype of new_node() in scope. That being said, it's preferable you post code that people can use to reproduce your issue. Commented Jun 24, 2011 at 17:13
  • Shouldn't it be function f_lock *new_node() not struct f_lock *new_node()? Commented Jun 24, 2011 at 17:20
  • 2
    @Mike: in C, function is not reserved and has no special meaning. The original definition is correct. Without a typedef, the name of the type is struct f_lock; f_lock by itself is not defined Commented Jun 24, 2011 at 18:21
  • possible duplicate of Assignment makes pointer from integer without cast Commented Sep 14, 2014 at 17:55

2 Answers 2

6

Did you also get the error implicit declaration of function ‘new_node’? Because in that case, you probably forgot to declare new_node in the module where you're calling it. (If you're not getting that error, recompile with -Wall to turn more warnings on.)

Explanation of the warning message: if new_node has not been declared properly, the compiler will assume it returns int, which is a kind of default return type (for historical reasons).

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

3 Comments

I din't get the error but probably that is the case. For I have noth functions in the same module
@Juggler: It is almost always a good idea to turn compiler warnings up to maximum.
See also -Werror in the GCC manual, and use it to your advantage.
1

I seem to remember getting this (rather misleading) error message once when I had forgotten to declare a function as taking void which is required in c (but not c++) for functions with no parameters:

struct f_lock *new_node()
{
   struct f_lock *new_f_lock;
   .....
   return new_f_lock;
}

Should be:

struct f_lock *new_node(void)
{
   struct f_lock *new_f_lock;
   .....
   return new_f_lock;
}

Comments

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.