Function Pointer

What is the difference between

1
2
int (*callback)(double,float);
callback = MyFunc;


and

1
2
int (*callback)(double,float);
callback point_to_func = MyFunc;
The first line declares a pointer variable called "callback"

The second line in the first example assigns a value to that pointer (and if MyFunc is a suitable function, it will compile):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>

int MyFunc(double, float)
{
    std::cout << "hi\n";
    return 0;
}

int main()
{
    int (*callback)(double,float); // declare a variable
    callback = MyFunc; // assign
    callback(1,2); // use
}


In the second example, the second line doesn't make sense: it treats "callback" as a type. Now if someone used typedef in the first line, to declare callback as a type, it would have worked:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>

int MyFunc(double, float)
{
    std::cout << "hi\n";
    return 0;
}

int main()
{
    typedef int (*callback)(double,float); // declare a type
    callback point_to_func = MyFunc; // declare and initialize a variable
    point_to_func(1,2); // use
}


Last edited on
I see I see. I've been moving on to hooks and procedures since almost everything in the windows SDK relies on them. Hooking functions inherently requires confidence with function pointer notation and typedeffing (the latter is what confuses me from time to time).

Much appreciated.
Topic archived. No new replies allowed.