發表文章

目前顯示的是 11月, 2009的文章

Warning: left-hand operand of comma expression has no effect!

首先來看看下面這隻 Macro: # define UTL_LIST_ALL_ELEMENTS_RO(list, node, data) \ ______ ( (node) = UTL_LIST_HEAD(list) ) ; \ ______ (node) != NULL && ( (data) = UTL_LIST_DATA(node) , 1 ) ; \ ______ ( (node) = UTL_LIST_NEXTNODE(node) ) 很簡單的 Macro,作用是 Link List 的 Traversal,給 For-Loop 使用。 那為甚麼要特別拿出來討論?? 重點在 ( (data) = UTL_LIST_DATA(node) , 1 ) 中的 ", 1" ,那到底有甚麼作用呢? 寫一個小程式來看看: #include #include int main() { @@ int a = 0, i = 10, j = 20; @@ printf( "a = %d\n", ( a = ( i, j ) ) ); @@ return 0; } 結果印出來的答案是 a = 20 事實上,在編譯的時候,如果是 gcc -Wall test.c 的話,我們會發現下面的警告: test.c: In function ‘main’: test.c:7: warning: left-hand operand of comma expression has no effect 也就是說,在 C 的語法裡面 (a,b,c,...,n) 的語法裡面,事實上就等於是 n ,逗號左邊的全部數值是沒有作用的!! 既然沒有作用,那為甚麼 Macro 會出現這樣的寫法?? 我們在看看下面的另一個小程式 test2.c #include #include int main() { @@ int a = 0, i = 10, j = 20; @@ printf( "a = %d\n", ( a = ( i=i+50, j ) ) ); @@ printf( "i = %d\n", i ); @@ return 0; } 最後會印出 a = 20 i = 60 所以, 即使在 ( a =