C11:6.5.2.2 p9:
If the function is defined with a type that is not compatible with the type (of the expression) pointed to by the expression that denotes the called function, the behavior is undefined.
As far as I know,The behavior of the function call expression depends on whether the prototype of the function being called is in scope at the point of call.
Then for "the type (of the expression) pointed to by the expression that denotes the called function", doesn't the type pointed to by the expression of the called function just be the function type, that is, the function declaration?
To be frank, in my opinion, isn't the intention of the standard just that "function declarations should be compatible with definitions"? And, 6.2.7 p2 has already mentioned:
All declarations that refer to the same object or function shall have compatible type; otherwise, the behavior is undefined.
Then does this mean that 6.5.2.2 p9 is redundant?, or maybe there is a problem with my understanding? Thank you for your reading and answering.
Section 6.5.2.2 deals with function calls, which is the context of the above quote. The compatibility of declarations is covered elsewhere.
Paragraph 9 in particular deals with the expression used to call a function. This could be a simple declaration or a function pointer. For example:
int foo(char *p)
{
// do something with p
return 1;
}
int main()
{
void (*fp)(void) = (void (*)(void))foo;
fp();
return 0;
}
The function call fp
is undefined behavior as per 6.5.2.2p9 because the function foo
is being called through an incompatible function pointer type.