免费注册 查看新帖 |

Chinaunix

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

C语言问题请教 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-11-06 14:11 |只看该作者 |倒序浏览
在看《unix环境高级编程(第二版)》中的程序4-7递归降序遍历目录层次结构时,里面有个dopath()递归函数,函数内容如下所示:
static int dopath(Myfunc * func)
{
        struct stat statbuf;
        struct dirent *dirp;
        DIR *dp;
        int ret;
        char *ptr;
        if(lstat(fullpath,&statbuf)<0)
                return(func(fullpath,&statbuf,FTW_NS));
        if(S_ISDIR(statbuf.st_mode)==0) /*not a directory*/
                return (func(fullpath,&statbuf,FTW_F));
        if((ret=func(fullpath,&statbuf,FTW_D))!=0)
                return(ret);
        ptr=fullpath+strlen(fullpath);
        *ptr++='/';
        *ptr=0;
        if((dp=opendir(fullpath))==NULL)
                return(func(fullpath,&statbuf,FTW_DNR));
        while((dirp=readdir(dp))!=NULL)
        {
                if(strcmp(dirp->d_name,".")==0||strcmp(dirp->d_name,"..")==0)
                        continue;
                strcpy(ptr,dirp->d_name);
                if((ret=dopath(func))!=0)
                        break;
        }       
        ptr[-1]=0;
        if(closedir(dp)<0)
                err_ret("can't close directory %s",fullpath);
        return (ret);
}

看不懂ptr[-1]=0这句语句是什么意思,数组的索引可以用负数吗?不是从0开始吗,还是这根本不是数组??
请高手指教,谢谢!

论坛徽章:
0
2 [报告]
发表于 2006-11-08 17:59 |只看该作者
ptr[-1]=0;就是相当于当前ptr指针*(ptr-1)。

[ 本帖最后由 daxialht 于 2006-11-8 18:03 编辑 ]
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP