简体   繁体   English

C [-Wincompatible-pointer-types] 如何转换

[英]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.

相关问题 -Wincompatible-pointer-types和enum - -Wincompatible-pointer-types and enum 警告:C 中不兼容的指针类型 [-Wincompatible-pointer-types] - warning: incompatible pointer types [-Wincompatible-pointer-types] in C C中的函数指针:警告:来自不兼容指针类型[-Wincompatible-pointer-types]的赋值 - Function pointers in C: warning: assignment from incompatible pointer type [-Wincompatible-pointer-types] 来自不兼容指针类型的赋值[Wincompatible-pointer-types] - Assignment from incompatible pointer type[Wincompatible-pointer-types] 编译时获取结构指针警告的 -Wincompatible-pointer-types - Getting -Wincompatible-pointer-types for struct pointer warning when compiling 警告:从不兼容的指针类型返回 [-Wincompatible-pointer-types]| - warning: return from incompatible pointer type [-Wincompatible-pointer-types]| 来自不兼容的指针类型[-Wincompatible-pointer-types]的赋值 - assignment from incompatible pointer type [-Wincompatible-pointer-types] 如何修复将&#39;char [16]&#39;传递给&#39;FILE *&#39;类型的参数(又名&#39;struct __sFILE *&#39;)的不兼容指针类型[-Wincompatible-pointer-types] - how to fix incompatible pointer types passing 'char [16]' to parameter of type 'FILE *' (aka 'struct __sFILE *') [-Wincompatible-pointer-types] 当我运行我的 C 程序时,我的编译器返回“从不兼容的指针类型‘char *’[-Wincompatible-pointer-types] 分配给‘int *’” - My compiler returned "assignment to 'int *' from incompatible pointer type 'char *' [-Wincompatible-pointer-types]" when I ran my C program 在 c 中将大端转换为小端。 警告:从不兼容的指针类型返回 [-Wincompatible-pointer-types] 返回 src; - converting Big endian to little endian in c. warning:return from incompatible pointer type [-Wincompatible-pointer-types] return src;
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM