免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2303 | 回复: 2
打印 上一主题 下一主题

关于fdopendir 的 奇怪 warning [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-05-01 14:32 |只看该作者 |倒序浏览
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

int
main()
{
&nbsp;&nbsp;&nbsp;&nbsp;int fd;
&nbsp;&nbsp;&nbsp;&nbsp;DIR *dir;
&nbsp;&nbsp;&nbsp;&nbsp;struct dirent *dir_str;
&nbsp;&nbsp;&nbsp;&nbsp;fd = open("/",O_RDONLY);
&nbsp;&nbsp;&nbsp;&nbsp;dir = opendir("/");
&nbsp;&nbsp;&nbsp;&nbsp;dir = fdopendir(fd);  //用opendir 则没有warning

&nbsp;&nbsp;&nbsp;&nbsp;while ((dir_str = readdir(dir)) != NULL){
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf("%s\n",dir_str->d_name);
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;closedir(dir);
&nbsp;&nbsp;&nbsp;&nbsp;close(fd);
&nbsp;&nbsp;&nbsp;&nbsp;

&nbsp;&nbsp;&nbsp;&nbsp;return 0;

}


gcc fdopendir.c
得到
fdopendir.c: In function 'main':
fdopendir.c:17: warning: assignment makes pointer from integer without a cast

可是man opendir

RETURN VALUE
       The opendir() and fdopendir() functions return a  pointer  to  the  directory
       stream.  On error, NULL is returned, and errno is set appropriately.
绝不是integer
十分不解

论坛徽章:
0
2 [报告]
发表于 2008-05-01 15:33 |只看该作者
我的系统man opendir
并没有你提到的fdopendir()这个函数啊
RETURN VALUE
       The opendir() and fdopendir() functions return a  pointer  to  the  directory
       stream.  On error, NULL is returned, and errno is set appropriately.


如果是因为fdopendir()这个函数不存在
则出现warning提示是因为: 没有定义的函数有个默认的返回值int

论坛徽章:
0
3 [报告]
发表于 2008-05-01 16:55 |只看该作者
原帖由 zhuhefang2006 于 2008-5-1 15:33 发表
我的系统man opendir
并没有你提到的fdopendir()这个函数啊
RETURN VALUE
       The opendir() and fdopendir() functions return a  pointer  to  the  directory
       stream.  On error, NULL is ret ...

VERSIONS
       fdopendir() is available in glibc since version 2.4.

CONFORMING TO
       opendir() is present on SVr4, 4.3BSD, and specified in POSIX.1-2001.
       fdopendir() is under consideration for inclusion in the next version
       of POSIX.1.

FreeBSD 尚未实现呢
不知道楼上用的哪种类 UNIX
我的gcc
$gcc -v
Using built-in specs.
Target: i686-pc-linux-gnu
Configured with: /var/tmp/portage/sys-devel/gcc-4.1.2/work/gcc-4.1.2/configure --prefix=/usr --bindir=/usr/i686-pc-linux-gnu/gcc-bin/4.1.2 --includedir=/usr/lib/gcc/i686-pc-linux-gnu/4.1.2/include --datadir=/usr/share/gcc-data/i686-pc-linux-gnu/4.1.2 --mandir=/usr/share/gcc-data/i686-pc-linux-gnu/4.1.2/man --infodir=/usr/share/gcc-data/i686-pc-linux-gnu/4.1.2/info --with-gxx-include-dir=/usr/lib/gcc/i686-pc-linux-gnu/4.1.2/include/g++-v4 --host=i686-pc-linux-gnu --build=i686-pc-linux-gnu --disable-altivec --enable-nls --without-included-gettext --with-system-zlib --disable-checking --disable-werror --enable-secureplt --disable-multilib --enable-libmudflap --disable-libssp --disable-libgcj --with-arch=i686 --enable-languages=c,c++,treelang,fortran --enable-shared --enable-threads=posix --enable-__cxa_atexit --enable-clocale=gnu
Thread model: posix
gcc version 4.1.2 (Gentoo 4.1.2 p1.1)
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP