Handmade Hero»Forums»Game
Dries Lamberechts
2 posts
Can't seem to understand the function pointers
Hi everyone,

I'd like for someone to explain me how the #define and typedef etc work for the function pointers Casey makes.
Here's the code I'm talking about:

1
2
3
4
5
6
#define X_INPUT_SET_STATE(name) DWORD WINAPI name(DWORD dwUserIndex, XINPUT_VIBRATION *pVibration)
typedef X_INPUT_SET_STATE(x_input_set_state);
X_INPUT_SET_STATE(XInputSetStateStub)
{
    return(ERROR_DEVICE_NOT_CONNECTED);
}


He uses these a lot and I can't figure out how it works.
I know what both do separately (define and typedef) but I don't know how he makes them work together AND while adding the method with the stub.

I'm a C++ and C# programmer (recently graduated) and function pointers have been discussed briefly and I really like to know how it works. Casey's way of doing it does seem different from what I've seen before. Is there a specific idea behind his way?

Thanks in advance
Mārtiņš Možeiko
2559 posts / 2 projects
Can't seem to understand the function pointers
Edited by Mārtiņš Možeiko on
#define is simple text-replacement. C/C++ preproecssor doesn't care what is inside #define, it just expands it. So from that code X_INPUT_SET_STATE define simply expands two times:
1
2
3
4
5
typedef DWORD WINAPI x_input_set_state(DWORD dwUserIndex, XINPUT_VIBRATION *pVibration);
DWORD WINAPI XInputSetStateStub(DWORD dwUserIndex, XINPUT_VIBRATION *pVibration)
{
    return(ERROR_DEVICE_NOT_CONNECTED);
}

One is function pointer typedef. And second one is simple function. Do you understand how these two are used?

Casey simply uses #define because he doesn't want to type two times return type and all the arguments. Which need to be equal for function pointer type (typedef) and actual function it will point to (that Stub).
Dries Lamberechts
2 posts
Can't seem to understand the function pointers
I think I got it now :) Thanks for the answer