gpt4 book ai didi

c - typedef 函数指针 -> 从不兼容的指针类型初始化

转载 作者:太空宇宙 更新时间:2023-11-04 07:14:19 26 4
gpt4 key购买 nike

我无法理解此警告的原因:

menu-file-select.c:41:29: warning: initialization from incompatible pointer type

违规代码是:

typedef int (*FileSelectFilter)(const char*, struct dirent*);

typedef struct {
const char *dir; //the directory path to read
const char *out; //where to copy the selected path
int outLen; //length of out buffer
FileSelectFilter *filter; //optional filter function
} FileSelectParams;

void showFileSelectMenu(FileSelectParams *params) {
/* ... */
FileSelectFilter filter = params->filter; // <-- warning generated here.
if(filter && !filter(path, ent)) continue;
/* ... */
}

int main(int argc, char **argv) {
/* ... */
FileSelectParams fsel = {
.dir = setting.lastpath,
.out = RomPath,
.outLen = sizeof(RomPath) - 1,
.filter = FileSelectFilter_Roms,
};
showFileSelectMenu(&fsel);
/* ... */
}

int FileSelectFilter_Roms(const char *path, struct dirent *file) {
/* ... */
}

据我所知,FileSelectFilter_Roms 匹配 FileSelectFilter typedef,所以我不明白为什么有人告诉我该类型不兼容。该程序似乎无论如何都可以运行,但是这里的警告让我很困扰。

最佳答案

您已将 FileSelectFilter 定义为指向 typedef 中函数的指针:

typedef int (*FileSelectFilter)(const char*, struct dirent*);

FileSelectPararms 中,您将 filter 成员定义为:

FileSelectFilter *filter;

这意味着filter实际上是一个指向函数指针的指针。这就是您在赋值 FileSelectFilter filter = params->filter; 中收到错误的原因。

filter 成员应该简单地定义为:

FileSelectFilter filter;

关于c - typedef 函数指针 -> 从不兼容的指针类型初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26120837/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com