[問題] --i 和i--的不同
目前在看Cracking the Coding Interview
有一些問題想請教大家
這是用c語言寫的
其中有一題是這樣的
Q:Implement a function void reverse(char* str) in C
which reverse a null-terminated string.
Answer:
void reverse(char *str){
char* end=str;
char tmp;
if (str){
while(*end){ /*find end of the string */
++end;
}
--end; /* set on char back, since last char is null*/
/*swap characters from start of string with the end of the
*string, until the pointers meet in the middle. */
while (str <end) {
tmp = *str;
*str++ = *end;
*end-- = tmp;
}
}
}
我有兩個問題:
1. --end 和 end-- 是哪裡不同
2. str< end 是在比較位址的大小嗎 還是pointer 在位址的先後順序
問很基礎的問題,真是不好意思 :p
--
恬靜,閑適安靜,很美好的寧靜
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 135.0.163.188
推
03/12 01:36, , 1F
03/12 01:36, 1F
→
03/12 01:37, , 2F
03/12 01:37, 2F
推
03/12 03:08, , 3F
03/12 03:08, 3F
→
03/12 03:09, , 4F
03/12 03:09, 4F
→
03/12 03:09, , 5F
03/12 03:09, 5F
→
03/12 03:09, , 6F
03/12 03:09, 6F
→
03/12 07:28, , 7F
03/12 07:28, 7F
→
03/12 07:29, , 8F
03/12 07:29, 8F
推
03/12 09:44, , 9F
03/12 09:44, 9F
推
03/12 13:19, , 10F
03/12 13:19, 10F
推
03/13 00:37, , 11F
03/13 00:37, 11F
→
03/13 00:39, , 12F
03/13 00:39, 12F
→
03/13 00:40, , 13F
03/13 00:40, 13F
→
03/13 00:42, , 14F
03/13 00:42, 14F