我对scandir()有一个问题:这个手册包含了这个原型:
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,我的编译器如下:
-Wall -Wpointer-arith -Wstrict-prototypes -Wunused -Wshadow -std=gnu99这个警告的意思是什么?RubyCompare的声明在我看来是正确的,除了警告之外,代码完全工作。
发布于 2008-09-28 17:55:23
实际上,不存在不能传递指向内联函数的指针的约束。内联关键字仅作为编译器在可能时进行内联调用的提示。
问题是scandir()的手册有点误导。第四个参数的原型实际上是int (*cmp)(const *,const *)。
因此,您需要修改代码如下:
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);发布于 2009-09-14 21:48:46
这个原型实际上已经在最近版本的GNU libc中进行了更改,以反映POSIX标准。
如果您有要同时处理旧代码和新代码的代码,请使用__GLIBC_PREREQ宏,如下所示
#define USE_SCANDIR_VOIDPTR
#if defined( __GLIBC_PREREQ )
# if __GLIBC_PREREQ(2,10)
# undef USE_SCANDIR_VOIDPTR
# endif
#endif
#ifdef USE_SCANDIR_VOIDPTR
static int RubyCompare(const void *a, const void *b)
#else
static int RubyCompare(const struct dirent **a, const struct dirent **b)
#endif..。
发布于 2008-09-28 17:32:08
你给它一个指向内联函数的指针?这是没有道理的,事实上,我想知道它的编译甚至只有一个警告。
编辑:上面的克里斯是对的,当内联关键字没有意义/不适用时,它就会被默默地忽略。
https://stackoverflow.com/questions/146291
复制相似问题