LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1232|回复: 3

C语言问题的While循环的跳出条件。

[复制链接]
发表于 2010-3-18 09:50:30 | 显示全部楼层 |阅读模式
/*
** If the string "substr" appears in "str", delete it.
*/
#define NULL    0              /* null pointer */
#define NUL    ’\0’            /* null byte */
#define TRUE    1
#define FALSE  0
/*
**      See if the substring beginning at ’str’ matches the string ’want’. If
**      so, return a pointer to the first character in ’str’ after the match.
*/
    char *
match( char *str, char *want )
{
    /*
     ** 把*str跟want中的字符比较直到want完结
     */
    while( *want != NUL )
        if( *str++ != *want++ )
            return NULL;
    return str;
}
    int
del_substr( char *str, char const *substr )
{
    char    *next;
    /*
     ** Look through the string for the first occurrence of the substring.
     */
    while( *str != NUL )
    {
        next = match( str, substr );
        if( next != NULL )
            break;
        str++;
    }
    /*
     ** If we reached the end of the string, then the substring was not
     ** found.
     */
    if( *str == NUL )
        return FALSE;
    /*
     ** Delete the substring by copying the bytes after it over the bytes of
     ** the substring itself.
     */
    while( *str++ = *next++ )//这里,以什么作为跳出while循环的条件?--------------------------------??
        ;
    return TRUE;
}

当*next为0的时候,为\0的时候也是为0的时候么。
谷歌说:计算C风格字符串长度一定要算上终结符null
记得NULL就是为0。
那这里的\0跟0是什么关系。
谢谢。
发表于 2010-3-18 11:55:33 | 显示全部楼层
\0要用单双引号括起来才能表示数值0,而0不用引号括起来表示数值0,用了引号表示ASCII码的字符0。
回复 支持 反对

使用道具 举报

发表于 2010-3-18 13:45:33 | 显示全部楼层
Post by 小锐同学;2076136
/*
** If the string "substr" appears in "str", delete it.
*/
#define NULL    0              /* null pointer */
#define NUL    ’\0’            /* null byte */
#define TRUE    1
#define FALSE  0
/*
**      See if the substring beginning at ’str’ matches the string ’want’. If
**      so, return a pointer to the first character in ’str’ after the match.
*/
    char *
match( char *str, char *want )
{
    /*
     ** 把*str跟want中的字符比较直到want完结
     */
    while( *want != NUL )
        if( *str++ != *want++ )
            return NULL;
    return str;
}
    int
del_substr( char *str, char const *substr )
{
    char    *next;
    /*
     ** Look through the string for the first occurrence of the substring.
     */
    while( *str != NUL )
    {
        next = match( str, substr );
        if( next != NULL )
            break;
        str++;
    }
    /*
     ** If we reached the end of the string, then the substring was not
     ** found.
     */
    if( *str == NUL )
        return FALSE;
    /*
     ** Delete the substring by copying the bytes after it over the bytes of
     ** the substring itself.
     */
    while( *str++ = *next++ )//这里,以什么作为跳出while循环的条件?--------------------------------??
        ;
    return TRUE;
}

当*next为0的时候,为\0的时候也是为0的时候么。
谷歌说:计算C风格字符串长度一定要算上终结符null
记得NULL就是为0。
那这里的\0跟0是什么关系。
谢谢。


'\0' == 0

这两者相等。但是一个有引号,一个没有引号。

另外,NULL 在 C++ 中就是 0,在 C 中,NULL 是 (void *)0,这两者有区别,C++的作者说过。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2010-3-18 15:20:00 | 显示全部楼层
谢谢,知道了。
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表