Handmade Hero » Forums » Code » Function Pointer Assignment Trick
Jesse James Lactin
4 posts
#13348 Function Pointer Assignment Trick
12 months ago

There's a trick I read about on the man page of dlopen. There is another way to assign to a function pointer by way of void * (like dlopen and GetProcAddress return). Instead of casting the return type, you cast the variable you're assigning to. It's less code than what Casey used on stream. It's A-OK according to POSIX, so it should work pretty much anywhere. The big advantage is assigning to a struct or an array of function pointers, because you can do this in a loop.

*(void **)(&function_pointer) = GetProcAddress("function_name");
Simon Anciaux
468 posts
#13360 Function Pointer Assignment Trick
11 months, 4 weeks ago

Thanks, that can be useful.