Search code examples
cvisual-c++gccfunction-pointersnamed-parameters

C - function pointer types with named parameters


On MSVC and gcc (GCC) 4.8.3 20140911 the following compiles and runs just fine:

#include <stdio.h>

int func(int a, int b){
    return 0;
}

int main(void){
    int (*funcPointer)(int a, int b);

    funcPointer = func;

    printf("funcPointer = %p\n", funcPointer);

    return 0;
}

Is such behaviour well-defined, or is it non-standard and it's actually illegal for function pointer types to have named parameters (i.e. names as well as types in their parameter list)?


Solution

  • You can have a parameter in your function pointer. It is totally valid. The parameter list matches that of the function being called and the names is just optional.

    It can also be written as

    int (*funcPointer)(int,int);
    

    I mean

    int (*funcPointer)(int a, int b);
    

    This is valid and you can verify the same by calling

    int res = funcPointer(3,4);
    

    and returning

    int func(int a, int b){
        return a+b;
    }