读书人

一个看不懂的玩意儿…解决思路

发布时间: 2012-04-02 19:58:59 作者: rapoo

一个看不懂的玩意儿……
老谭的书都看完了,在看C和指针,第4章《语句》习题7的答案没看明白,代码如下:

C/C++ code
/*** Shrink runs of white space in the given string to a single space.*/#define NUL     ’\0’voiddeblank( char *string ){    char    *dest;    char    *src;    int     ch;    /*    ** Set source and destination pointers to beginning of the string, then    ** move to 2nd character in string.    */    src = string;    dest = string++;    /*    ** Examine each character from the source string.    */    while( (ch = *src++) != NUL ){        if( is_white( ch ) ){        /*        ** We found white space.  If we’re at the beginning of        ** the string OR the previous char in the dest is not        ** white space, store a blank.        */            if( src == string || !is_white( dest[1] ) )            *dest++ = ’ ’;        }        else {            /*            ** Not white space: just store it.            */            *dest++ = ch;        }        }    *dest = NUL;}intis_white( int ch ){return ch == ’ ’ || ch == ’\t’ || ch == ’\v’ || ch == ’\f’ || ch == ’\n’|| ch == ’\r’;}


其中!is_white( dest[1] )这句看不懂是啥意思,在下愚笨,也没有看出答案的思路,敬请高人指点,愿闻其详!


[解决办法]
dest[-1]为获取dest指针当前指向位置的前一个字符

读书人网 >C++

热点推荐