Handmade Hero » Forums » Code » Function Pointer Assignment Trick
Jesse James Lactin
4 posts
#13348 Function Pointer Assignment Trick
2 weeks, 6 days 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
283 posts
#13360 Function Pointer Assignment Trick
2 weeks, 4 days ago

Thanks, that can be useful.