我有以下代码来打印 unix 中的目录列表。
struct dirent *res;
struct DIR *dir;
scanf("%s",str);
dir=opendir(str);
if(dir==NULL)
{
perror("Invalid directory");
return 1;
}
res=(struct dirent *)readdir(dir);
while(res)
{
printf("%s\n",res->d_name);
res=(struct dirent *)readdir(dir);
}
当我编译上面的代码时,我收到以下警告
ls.c:16:17: warning: passing argument 1 of ‘readdir’ from incompatible pointer type
[enabled by default]
/usr/include/dirent.h:164:23: note: expected ‘struct DIR *’ but argument is of type
‘struct DIR *’
ls.c:20:21: warning: passing argument 1 of ‘readdir’ from incompatible pointer type
[enabled by default]
/usr/include/dirent.h:164:23: note: expected ‘struct DIR *’ but argument is of type
‘struct DIR *’
当 GCC 说“预期的参数foo
但参数是类型foo
”时,它到底是什么意思?
我也尝试过使用struct DIR dir
而不是*dir
和&dir
而不是dir
,但它会导致以下错误
ls.c:7:12: error: storage size of ‘dir’ isn’t known
PS:代码的输出是完全OK的。