《C和指针》书中第8章习题4的不解
答案给的是这样的
char buffer[SIZE];
char *front, *rear;
...
front = buffer;
rear = buffer + strlen( buffer ) – 1;
while( front < rear ){
if( *front != *rear )
break;
front++;
rear––;
}
if( front >= rear ){
printf( "It is a palindrome!\n" );
}
This question borders on entrapment! If you try and get fancy like this:
if( *front++ != *rear–– )
break;
而答案特别提示:If you try and get fancy like this:
if( *front++ != *rear–– )
break;
the program can fail because the test after the loop ends is no longer valid.
我不明白,实际上,我编写这段代码的时候确实用的是他给出警告的那种,而结果竟然是正确的。希望高人给指点下!
[解决办法]
题目呢>???
[解决办法]
if( *front != *rear )
break;
front++;
rear––;
}
这个, 如果 *front == *rear, 即判断不成立; 那么front++和rear--不会执行; 而if( *front++ != *rear––)无论判断成不成立都能执行。 至于你遇到的情况是特殊情况, 也就是, *front != *rear始终成立。
很明显是错误的, 这个很简单。