Code value:
- Take the address
fptr . The type of this expression is a pointer to a pointer to a function (of a specific type). - Put this pointer expression on "pointer to void pointer".
- Separate the pointer to access the
fptr object as if it were an object of type void * . - Assign the result of the right to the value of l obtained in step 3.
Unfortunately, the one who wrote this example in POSIX was on the crack because step 3 violates the C alias rules and thus causes undefined behavior. In particular, real-world compilers will optimize this code in ways that violate the intended use.
What the author of this example sought was to not discard the right-hand side from the pointer to void to the pointer to the function. This is based on the claim that the C standard requires this actor to generate a warning, but I carefully searched for this requirement and did not find such a requirement.
If such a problem really exists (warning requirement), then the only way to turn off the warning without causing undefined behavior (for example, a bad example in the POSIX text) is as follows:
void (*fptr)(); // or whatever function pointer type you want void *temp = dlsym(handle, "my_function"); memcpy(&fptr, &temp, sizeof fptr);
source share