作者timrau (没code没真相)
看板EE_DSnP
标题Re: [问题] addHistory function
时间Sun Mar 30 23:14:23 2008
※ 引述《fairyflame (妖精火焰)》之铭言:
: 这个应该跟作业无关 我只是纯粹想知道code写的意义而已@__@"
: 在addhistory这个function里
: // remove ' ' at the end
: char* tmp = _readBufEnd - 1;
: while ((tmp >= _readBuf) && (*tmp == ' ')) *(tmp--) = 0;
: // remove ' ' in the beginning
: tmp = _readBuf;
: while (*tmp == ' ') ++tmp;
: // add to _history
: if (*tmp != 0) {
: addHistoryStr(tmp);
: _history[++_historySize].clear();
: _historyIdx = _historySize;
: }
: 在remove ' 'at the end里
: *(tmp--) = 0;的意义到底是什麽呢@@?
: 是作为一个flag当add history时不要把它给加进去
: 那为什麽remove ' 'in the beginning 就不用令*(tmp)=0呢?
因为这里的做法是直接改动"字串开头的地方"
不然*tmp = 0就直接把字串清空了(开头即是结尾)
: 当判断式if (*tmp != 0)时 那其它原本输入若有0的字串不会有问题吗@@"
: 虽然跟作业不太有太大的关系 但还是想知道为什麽想这样写
: 恳请开示 谢谢<(_ _)>
Assume at the beginning
"abcdefg
0" where
0 means '\0'
^ ^-- _readBufEnd
_readBuf
after char* tmp = _readBufEnd - 1;
"abcdefg
0"
^-- tmp
while ((tmp >= _readBuf) && (*tmp == ' ')) is true now, so *(tmp--) = 0;
which means,
replace the char pointed by tmp by 0, and then decrease tmp
by 1
"abcdefg
00"
^-- tmp
Again, the "while ..." is true, and then
"abcdefg
000"
^-- tmp
Now, the "while ..." becomes false.
Since the string should be terminated by '\0',
the string becomes "abcdefg" now.
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 140.112.5.50
※ 编辑: timrau 来自: 140.112.5.50 (03/30 23:18)
1F:推 fairyflame:感谢饶神的开示<(_ _)> 03/31 13:47
2F:推 battlecruise:请问 '\0' 跟 0的意义是一样的吗? 04/01 22:35
3F:推 ric2k1:char ch = '\0' 与 ch = 0 是一样的; 但和 ch = '0' 不同 04/01 23:32