Search code examples
ccompiler-warningsfunction-prototypes

Warning about function prototype even when the function takes arguments


I have a simple function in C which takes int* argument and I have the declaration of it in the header file like this:

void mapAuditFioError(int *errno);

But compiler cirbs compiling the files including this header as follows:

"warning: function declaration isn't a prototype"

and also:

"warning: passing argument 1 of 'mapAuditFioError' from incompatible pointer type"

even though am invoking it with a integer pointer itself.

Browsing for this warning always talks about functions like this : int func() which should actually be declared as int func(void). But what is the problem in my function above?


Solution

  • This is because errno is a reserved name in C (free-standing implementations aside), used for communicating erroneous conditions from standard library functions. As the glibc documentation says:

    The names of all library types, macros, variables and functions that come from the ISO C standard are reserved unconditionally; your program may not redefine these names. All other library names are reserved if your program explicitly includes the header file that defines or declares them.

    You cannot use errno as a variable name. It could be declared as a macro, and often it is.

    For example on GCC and glibc on my Linux, if I include say <stdio.h> before the prototype,

    void foo(int *errno);
    

    is preprocessed into

    void foo(int *(*__errno_location ()));
    

    Now, that does compile without warnings because it is a legal construct; on your C compiler the errno behaves differently.


    To fix it, call your parameter something else, such as errcode or error_number.


    The C11 standard N1570 Committee Draft 7.5 on <errno.h> specifically says that

    If a macro definition is suppressed in order to access an actual object, or a program defines an identifier with the name errno, the behavior is undefined.