作者oohay (五黑)
看板C_and_CPP
標題Re: [問題] 面試遇到的一個問題
時間Mon Feb 4 09:32:17 2008
※ 引述《kesa (blush)》之銘言:
: ※ 引述《aoc90058 (我是新警察Orz...)》之銘言:
: : in Expressions 5:4: 'Between the previous and next sequence point a scalar
: : object shall have its stored value modified at most once by the evaluation of
: : an expression. Furthermore, the prior value shall be accessed only to
: : determine the value to be stored.'
: : But fortunately, there's a simple way to avoid it: Never write code that does
: : things it shouldn't do between sequence points, like try to write into the
: : same int, or other built-in scalar value, more than once in the same
: : expression."
: f(i++, i);-> undefined /* 沒什麼人會這樣搞自己 */
這個未定義是因為是因為函數的參數順序未定義.
: a[i++] = i;-> undefeind /*常有人這樣寫*/
......這是未定義嗎? 我記得是 a[i] = i++;
: 都錯在 "Furthermore, the prior value shall be accessed only to dterminie
: the value to be stored (i)"
: 同理 p = z++ + z;-> undefined. /* @@ 考題 */
很抱歉,這裏++只打算更新z一次,但是z不會被更新第二次,
所以請問,上述句子提到"請不要把一個量詞更新超過一次",z+++z符合此條件啊!!!
所以頂多就是++的效果會發生在非sequence point之處,好比變化只發生在時脈切換處.
但是,未定義行為發生在哪裏呢?
隱約記得加法左右邊取值順序是未定義的,或許這算數吧!
: 第二個 z 不是用來存 z
沒錯,這句話說對了. 所以你贊成或反對z+++z? :)
: *q++ = *p++ -> defined.
--
→ 鄉民A:灌C可以搞阿嬌啊?
推 鄉民B:我的Visual C++光碟哪裏去了?
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 218.160.114.86
→ oohay:a[i++]=i? a[i]=i++? 連這也是混淆點 02/04 10:39
→ sunneo:how ever,一個沒跟人team work的小鬼頭也敢來披頭大論 02/04 12:12
→ sunneo:你以為你披頭4啊... 02/04 12:12
→ sunneo:並不是知道這個就可以 「拍拍手 喔 好棒好棒」 02/04 12:13
→ sunneo:所以呢 你是來討戰的吧 02/04 12:19