简体   繁体   中英

About function pointer declaration

#include <stdio.h>

void func(int ,int);
void (*fp)();

int main()
{
    fp = func;
    fp(10,20);
}

void func(int a,int b)
{
    printf("%d %d\n",a,b);
}

In the above piece of code, the prototypes of function pointer and the function definition doesn't match. But still the code works. Please can any one help me to over come this doubt?

The declaration

void (*fp)();

says fp is a pointer to function that takes unspecified number of arguments, that returns void . So it's compatible with void func(int a,int b) {...} .

Declare it as:

void (*fp)(void);
void (*fp)(); // Pointer to function returning nothing, no argument info
void func(int ,int); // Function returning nothing with two intarguments

Paragraph 14 explains what the pointer is, the next paragraph explains why it fits.

6.7.6.3 Function declarators (including prototypes)

14 An identifier list declares only the identifiers of the parameters of the function. An empty list in a function declarator that is part of a definition of that function specifies that the function has no parameters. The empty list in a function declarator that is not part of a definition of that function specifies that no information about the number or types of the parameters is supplied.145)
15 For two function types to be compatible, both shall specify compatible return types.146) Moreover, the parameter type lists, if both are present, shall agree in the number of parameters and in use of the ellipsis terminator; corresponding parameters shall have compatible types. If one type has a parameter type list and the other type is specified by a function declarator that is not part of a function definition and that contains an empty identifier list, the parameter list shall not have an ellipsis terminator and the type of each parameter shall be compatible with the type that results from the application of the default argument promotions. If one type has a parameter type list and the other type is specified by a function definition that contains a (possibly empty) identifier list, both shall agree in the number of parameters, and the type of each prototype parameter shall be compatible with the type that results from the application of the default argument promotions to the type of the corresponding identifier. (In the determination of type compatibility and of a composite type, each parameter declared with function or array type is taken as having the adjusted type and each parameter declared with qualified type is taken as having the unqualified version of its declared type.)

Also of interest are the "future language directions", because they spell out that it's a backwards-compatibility feature.

6.11 Future language directions

6.11.6 Function declarators

1 The use of function declarators with empty parentheses (not prototype-format parameter type declarators) is an obsolescent feature.

You can ask GCC to warn about function declarations without prototype using -Wstrict-prototypes , though you get false positives too:

http://coliru.stacked-crooked.com/a/e9eb7a7d2e384b00

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM