问题描述
我正在处理的代码有一个函数,它有一个 void 类型的双指针,在函数指针中,双指针被类型转换为 void,并且该指针没有在其他任何地方使用.我找不到任何为什么这样做的地方.有人请说明一下.
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
...
}
推荐答案
回调的类型可能是 LIBSSH2 库 API 的一部分.库将每个参数传递给它期望回调需要的回调.无论那个参数是什么,这个特定的回调都不需要它.程序员有四种选择:
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:
他可以省略原型中的参数名称,将
void **abstract
替换为void **
.这使得试图理解他的代码的人必须查看 LIBSSH2 API 才能理解最后一个参数是什么.
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.
他可以注释掉参数名,像这样:void **/*abstract*/
.
He could comment out the parameter name, like this: void ** /*abstract*/
.
该程序员选择选项 3.
This programmer choose option 3.
就我个人而言,对于这种情况,我倾向于选择选项 4.我也希望看到这样的内容:
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.
这篇关于将 void 双指针类型转换为 void,为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!