gpt4 book ai didi

c - 联机帮助页 scandir() 原型(prototype)怪异

转载 作者:太空狗 更新时间:2023-10-29 17:21:05 27 4
gpt4 key购买 nike

我对 scandir() 有疑问:联机帮助页包含此作为原型(prototype):

int scandir(const char *dir, struct dirent ***namelist,
int (*filter)(const struct dirent *),
int (*compar)(const struct dirent **, const struct dirent **));

因此我有这个:

static inline int
RubyCompare(const struct dirent **a,
const struct dirent **b)
{
return(strcmp((*a)->d_name, (*b)->d_name));
}

调用如下:

num = scandir(buf, &entries, NULL, RubyCompare);

最后编译器这样说:

warning: passing argument 4 of ‘scandir’ from incompatible pointer type

编译器是gcc-4.3.2,我的CFLAGS如下:

-Wall -Wpointer-arith -Wstrict-prototypes -Wunused -Wshadow -std=gnu99

这个警告是什么意思? RubyCompare 的声明对我来说看起来是正确的,除了警告之外,代码完全有效。

最佳答案

实际上,不存在不能将指针传递给内联函数的限制。 inline 关键字仅用作提示编译器在可能时内联调用。

问题是 scandir() 的联机帮助页有点误导。第 4 个参数的原型(prototype)实际上是 int (*cmp)(const void *, const void *)。

因此您需要像这样更改代码:

static inline int RubyCompare(const void *a, const void *b)
{
return(strcmp((*(struct dirent **)a)->d_name,
(*(struct dirent **)b)->d_name));
}

不过,我实际上不确定您为什么要编写此函数,因为您可以使用提供的 alphasort 比较函数:

num = scandir(buf, &entries, NULL, alphasort);

关于c - 联机帮助页 scandir() 原型(prototype)怪异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/146291/

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