Say I have a function pointer with this definition:
void ( *pressFunc ) ( void*, void* );
And i did this function:
void functionWithOneArg ( void* testPtr );
And i did this
pressFunc = &functionWithOneArg;
One. Would C actually let me do this? ( Assigning a function with one argument to a function with two )
Two. If so, what would happen to the second argument that is passed the function when its called? Does it just get 'cut off' and only the first argument is passed? Can someone explain?
C will allow that, C++ won't (unless you use reinterpret_cast)
typically, C compilers issue a diagnostic:
"test.c", line 7: warning: assignment type mismatch:
pointer to function(pointer to void, pointer to void) returning void "=" pointer to function(pointer to void) returning void
"test.c", line 7.14: 1506-068 (W) Operation between types "void(*)(void*,void*)" and "void(*)(void*)" is not allowed.
test.c:7: warning: assignment from incompatible pointer type
Calling the function through the resulting pointer is undefined behavior.
in C: 6.5.2.2/9
"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"
in C++: 5.2.10[expr.reinterpret.cast]/6
"The effect of calling a function through a pointer to a function type that is not the same as the type used in the definition of the function is undefined"