[英]C [-Wincompatible-pointer-types] how to cast
my code: https://godbolt.org/z/de7fbdjh7我的代码: https ://godbolt.org/z/de7fbdjh7
code from source: https://stackoverflow.com/a/49072888/15603477源代码: https ://stackoverflow.com/a/49072888/15603477
Almost exact the same.几乎一模一样。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct
{
int iValue;
int kValue;
char label[6];
} my_data;
int cmp_mydata_ivalue(my_data* item1 , my_data* item2 )
{
if(item1->iValue < item2->iValue) return -1;
if(item1->iValue > item2->iValue) return 1;
return 0;
}
int main(void){
my_data datalist[256] = {0};
{
int i;
for(i = 0;i<20;i++){
datalist[i].iValue = i+100;
datalist[i].kValue = i+1000;
sprintf(datalist[i].label,"%2.2d", i+10);
}
}
printf("new line\n");
{
my_data srchitem = {105,1018,"13"};
my_data *foundItem = (my_data*) bsearch(&srchitem, datalist,20, sizeof(my_data),cmp_mydata_ivalue);
bsearch_results(&srchitem, foundItem);
}
}
The same question asked many times.同一个问题问了很多次。 But I don't know how to cast it.但是我不知道怎么投。
error code:错误代码:
*callback1.c: In function ‘main’:
callback1.c:58:89: warning: passing argument 5 of ‘bsearch’ from incompatible pointer type [-Wincompatible-pointer-types]
58 | my_data *foundItem = (my_data*) bsearch(&srchitem, datalist,20, sizeof(my_data),cmp_mydata_ivalue);
| ^~~~~~~~~~~~~~~~~
| |
| int (*)(my_data *, my_data *) {aka int (*)(struct <anonymous> *, struct <anonymous> *)}*
One way to try to use gcc option to supress the error.尝试使用 gcc 选项来抑制错误的一种方法。 Another way is somewhere I need to cast.另一种方法是我需要投射的地方。 But now i don't know how to cast.但现在我不知道怎么投。
Tutorial I found so far: https://www.tutorialspoint.com/c_standard_library/c_function_bsearch.htm到目前为止我找到的教程: https ://www.tutorialspoint.com/c_standard_library/c_function_bsearch.htm
The comparison function must have the type比较函数必须具有类型
int ( const void *, const void * )
See the declaration of the function bsearch
见函数bsearch
的声明
void *bsearch(const void *key, const void *base,
size_t nmemb, size_t size,
int (*compar)(const void *, const void *));
So you should declare and define your function like所以你应该声明和定义你的函数,比如
int cmp_mydata_ivalue( const void *a , const void *b )
{
const my_data *item1 = a;
const my_data *item2 = b;
if ( item1->iValue < item2->iValue) return -1;
if(item1->iValue > item2->iValue) return 1;
return 0;
}
Don't ever use workarounds to suppress errors/warnings from the compiler.永远不要使用变通方法来抑制来自编译器的错误/警告。 You should carefully understand them and fix the code instead.您应该仔细理解它们并修复代码。 If you chose to ignore them, you must be very conscious of what are the implications.如果您选择忽略它们,您必须非常清楚其中的含义。
Having said that, the bsearch prototype is the following:话虽如此,bsearch 原型如下:
void* bsearch( const void *key, const void *ptr, size_t count, size_t size,
int (*comp)(const void*, const void*) );
meaning it expects the last parameter to be a function pointer to a function with the following signature:这意味着它希望最后一个参数是指向具有以下签名的函数的函数指针:
int function(const void*, const void*);
What you are passing is a function of this kind你传递的是这种函数
int cmp_mydata_ivalue(my_data* item1 , my_data* item2 )
Which is uncompatible, as C can't do any implicit cast.这是不兼容的,因为 C 不能进行任何隐式转换。 You must rewrite your function to something like this:您必须将函数重写为如下内容:
int cmp_mydata_ivalue( const void *cvp_item1 , const void *cvp_item2 )
{
const my_data *item1 = (const my_data *)cvp_item1;
const my_data *item2 = (const my_data *)cvp_item2;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.