问题
The man page of dlsym()
lists
*(void **) (&cosine) = dlsym(handle, "cos");
as a workaround for casting the return value of dlsym()
.
What's the meaning of *(void **) (&cosine)
here? I understand cosine
is a function pointer defined previously, but I'm not sure why an ampersand &
is needed before the name (an error without &
). Moreover, I don't figure out why the pointer of void *
(void **
) is again used with *
.
回答1:
Let's unwrap it a bit at a time:
&cosine
This takes a pointer to the variable cosine
, so this will be a pointer to a function pointer.
(void **) &cosine
We cast the pointer-to-function-pointer to pointer-to-pointer-to-void.
* (void **) &cosine
We dereference the casted pointer, assigning the result of dlsym()
into it.
Effectively, what's happening is a side-step of the issue. Instead of casting the result of dlsym()
into the correct type of function pointer, we pretend that cosine
is a void *
(through a level of indirection) and assign to it.
来源:https://stackoverflow.com/questions/43509159/dlsym-workaround-return-type