问题
The code I'm working on has a function which has a double pointer of type void, in the function pointer the double pointer is typecasted to void, and the pointer is not used anywhere else. I cant find any where why this is done. someone please shed some light.
static void kbd_callback(const char *name, int name_len,
const char *instruction, int instruction_len,
int num_prompts,
const LIBSSH2_USERAUTH_KBDINT_PROMPT *prompts,
LIBSSH2_USERAUTH_KBDINT_RESPONSE *responses,
void **abstract /* <-- This */)
{
int i;
size_t n;
char buf[1024];
(void)abstract; // <---- and this
...
}
回答1:
The type of the callback is probably part of the API of a LIBSSH2 library. The library passes every parameter to the callback that it expects the callback will need. Whatever that parameter is, this particular callback doesn't need it. The programmer has four choices:
He can leave out the name of the parameter in the prototype, replacing
void **abstract
withvoid **
. This makes it so that someone trying to understand his code has to look at the LIBSSH2 API to understand what the last parameter is.He can just not use the parameter. But this will get him a warning from his compiler.
He can use the parameter in a way that has no consequence to hide the warning.
He could comment out the parameter name, like this:
void ** /*abstract*/
.
This programmer choose option 3.
Personally, I tend to prefer option 4 for this case. I'd also prefer to see something like this:
#define OK_UNUSED(a) if (false && (a)); else (void) 0
...
OK_UNUSED(abstract);
This makes it very clear that it's okay that the parameter is unused.
回答2:
It's a double pointer meaning it is a pointer to a pointer. Being of type void it can hold any type. So i'm assuming it will be used to hold a pointer, in this case i'm assuming the developer does not know what type of pointer it will be passed or it holds different types already declared and initialized in the program.
来源:https://stackoverflow.com/questions/58444728/typecasting-void-double-pointer-to-void-why