Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

callback function: difference between void(*func)(int) and void(func)(int)

So Lets say I have a function:

void foo (int i){
    cout << "argument is: " << i << endl;
}

And I am passing this function to:

void function1 (void(callback)(int), int arg){
    callback(arg);
}

void function2 (void(*callback)(int), int arg){
    callback(arg);
}

are these two functions identical? Is there any difference between the two?

like image 378
user3606309 Avatar asked Sep 30 '22 19:09

user3606309


1 Answers

The rule is that in a function's parameter list, a parameter declared to have a function type is adjusted to have pointer to function type (similarly, and probably more well-known, a parameter declared to have type "array of T" is adjusted to have type "pointer to T". Redundant parentheses in declarators are allowed, but ignored.

Thus, in

void function1 (void(callback)(int), int arg);
void function2 (void (*callback)(int), int arg);
void function3 (void callback(int), int arg);

The first parameter of those three functions have exactly the same type - "pointer to function of (int) returning void".

like image 145
T.C. Avatar answered Oct 03 '22 01:10

T.C.