我有以下代碼在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 *’
時,它說:「期望的參數foo
但參數是foo
型」是什麼GCC究竟意味着什麼呢?
我也嘗試使用struct DIR dir
代替*dir
和&dir
代替dir
,但它會導致以下錯誤
ls.c:7:12: error: storage size of ‘dir’ isn’t known
PS:代碼的輸出是完全OK。
它工作:)謝謝。 – rjv
順便說一句,我非常感興趣,我查了相關的語法......在C99中,至少,'struct struct foo'是非法的。 – Sneftel