views:

298

answers:

3

I'm getting a strange compile warning. It's intermittent, and doesn't appear every build. I get the warning "initialization makes pointer from integer without a cast" for the following line:

callbackTable *callbacks = generateLoggingCallback();

and, for completeness, this gives the same outcome

callbackTable *callbacks;
callbacks = generateLoggingCallback();

the function prototype for that is:

callbackTable *generateLoggingCallback();

and the implementation is

callbackTable *generateLoggingCallback() { ... }

So, I'm not quite sure what the problem is. Ideas?

A: 

Is the function generateLoggingSmfReaderCallback or generateLoggingCallback? If the function name in the prototype does not match the one in your call, the strange thing then is only that you are not getting the warning on each build.

Tarydon
Sorry, my copy-paste mistake. It _is_ the same function (see edit).
Joe
Hmmm... There is still a copy-paste mistake :)
Tarydon
More seriously, isn't there an option you can turn on to enforce strict checking of function prototypes (and raising errors when a function is called without a prototype). This style of flying blind without proper function prototypes seems fraught with danger. What if a function returned a double and you tried interpret it as an int? The compiler would not generate any warnings (as it would assume the unknown function was returning an int). Disaster.
Tarydon
Yes I was a bit worried about late binding. I'm still finding my C feet so I don't know what is and isn't normal. I'll check my tags. And I hope unit tests would catch such a case as you mention...
Joe
I usually work with C++, which has much tighter checking in general. Also, I generally compile with all warnings on and the warning level set to the highest. There is the occasional spurious warning, but that is better suppressed locally, than turning off that warning globally. I would say compiler warnings should be your first line of defense (much stronger than unit tests, since we all know we are never going to get down to writing tests for 100% coverage of all our code).
Tarydon
+3  A: 

If it's pure C, isn't there a warning about 'unknown' function? if yes, then the compiler decides that the unknown function returns int, and continues on.. check if proper headers are included, and the function is declared before it's used.

Yossarian
No 'unknown function' warning, but you're correct, thanks.
Joe
try enabling all warnings, for example -Wall in gcc. (/W4 in msvc)
Yossarian
+1. No warning required by C89 for this. Only for C99 (where it's not allowed). So your compiler may well not care about it :
Johannes Schaub - litb
+1  A: 

Found the answer, as per this. I wasn't referencing the header file containing the function prototype. So, as I understand it, the compiler was guessing at the function's type signature, and guessing the return type as the default int.

It all worked because the implementation file containing the function was included in the build and the return type (assumed to be an int) was just placed in a variable declared as a pointer.

Joe