尽管我的代码运行正常,但我收到了不兼容的指针类型错误警告。首先,这是我的代码的简化版本:
typedef struct {
int field1;
int field2;
} my_struct;
void some_function(void **data){
// do something with *data
}
int main(int argc, char *argv[]){
my_struct *ptr = malloc(sizeof(my_struct));
ptr->field1 = 5;
ptr->field2 = 17;
some_function(&ptr);
}
my_struct
类型是一个示例,但我实际上使用了多种类型,因此 some_func
必须接受 void**
,而不是我的结构**
。每次我以上述方式调用 some_func
时,我都会收到以下警告,但我的代码仍能正常运行。
warning: passing argument 1 of ‘my_func’ from incompatible pointer type
我不确定为什么这些类型不兼容,并且想以某种方式删除警告。对于答案,请随意解释为什么会发生这种情况,如果可能的话,如何删除警告(也许是 cpp 指令?)。谢谢!
最佳答案
使用 void *
参数类型。
void **
不是通用指针类型。 void *
是。
关于C: 为什么 &(void *) 和 void** 不兼容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20753213/