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

标签 c manpage

我对 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/

相关文章:

c++ - 尝试将线程创建方法从 C 迁移到 C++ 不起作用

C - 如何解决 "Address is 0 bytes after a block of size alloc' d"

linux - Git 命令执行(如 "git diff"或 "git help commit")返回关于 "sensible-paper"的错误

windows - Windows 的 'man' 程序(打开 UNIX 手册页的程序)在哪里?

shell - 着色手册页(使用 `less` 寻呼机)在 Cygwin MinTTY 中不起作用

c - 编写 GTK 应用程序的首选方法是什么?

c - 二叉树 - 插入和删除的问题

C 重置 FOR 循环中的数据计数器

ruby - 如何为 RDoc::usage() 定义没有 === 标题的部分?