海湾合作委员会给了我以下警告:
note: expected 'const void **' but argument is of type 'const struct auth **
Run Code Online (Sandbox Code Playgroud)
有没有可能导致问题的情况?
更大的片段是
struct auth *current;
gl_list_iterator_next(&it, ¤t, NULL);
Run Code Online (Sandbox Code Playgroud)
函数只存储在current一些void *指针中.
该错误信息是足够清晰:你传递一个struct auth **,其中一个void **被接受.这些类型之间没有隐式转换,因为它void*可能与其他指针类型的大小和对齐方式不同.
解决方案是使用中间体void*:
void *current_void;
struct auth *current;
gl_list_iterator_next(&it, ¤t_void, NULL);
current = current_void;
Run Code Online (Sandbox Code Playgroud)
编辑:为了解决下面的评论,这里有一个为什么这是必要的例子.假设你在一个平台上sizeof(struct auth*) == sizeof(short) == 2,同时sizeof(void*) == sizeof(long) == 4; 这是C标准所允许的,实际上存在具有不同指针大小的平台.那么OP的代码将类似于做
short current;
long *p = (long *)(¤t); // cast added, similar to casting to void**
// now call a function that does writes to *p, as in
*p = 0xDEADBEEF; // undefined behavior!
Run Code Online (Sandbox Code Playgroud)
但是,这个程序也可以通过引入一个中间体来工作long(虽然结果可能只有在long值足够小以存储在a中时才有意义short).