c - 如何摆脱预期的 'int(*)(const struct dirent *)' 但参数类型为 'int (*)(struct dirent *)' 错误?

标签 c struct

我不明白为什么我的 C 编译器会抛出一个严重的错误。我根据 man page 初始化了所有东西,或者至少我认为我做了。 .然而我仍然被抛出一个严重的错误。它一直说 expected int (*)(const struct dirent *) but argument is of type 'int (*)(struct dirent *)'

我的代码:

extern int alphasort();

int count, i;
struct direct **files;

if(!(getcwd(pathname, sizeof(pathname))))
{
    die("Error getting pathname\n");
}
printf("Current Working Directory = %s\n", pathname);
count = scandir(pathname, &files, file_select, alphasort);

if (count < 0)
{
    die("No files in this directory.\n");
}
else
{
    printf("Number of files = %d\n", count);
    for (i = 1; i < count+1; i++)
    {
        printf("%s  ",files[i-1]->d_name);
        printf("\n");
    }

    return 1;
}

pathname = char pathname[MAXPATHLEN];

文件选择 =

int file_select(struct direct *entry)
{
    if ((strcmp(entry->d_name, ".") == 0) || (strcmp(entry->d_name, "..") == 0))
        return (FALSE);
     else
         return (TRUE);
}

最佳答案

您需要更改回调函数,使其参数符合预期:

int file_select(const struct direct *entry)
{
    .
    .
    .
}

关于c - 如何摆脱预期的 'int(*)(const struct dirent *)' 但参数类型为 'int (*)(struct dirent *)' 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30182345/

相关文章:

c++ - C++中的结构和类有什么区别?

c - c程序中,如何使用Linux tee重定向自身的输出

C - 使用动态数组

c++ - 为什么 Clang 优化掉 x * 1.0 而不是 x + 0.0?

c - 链接列表有问题(添加和打印)

c++ - C++ 和 C 中的 union 初始化

c - 如何打印二叉树?

c - "struct a a1 = {0};"不同于 "struct a a2 = {5};"为什么?

c - 如何在 C 中创建结构

Matlab:结构中变量的名称