2015-12-18 47 views
-3

爲什麼不編譯這段代碼?Macro not compiling

#define container_of(ptr, type, field_name) (\ 
(type *) ((char *) (ptr) - offsetof(type, field_name))) 

錯誤

C:\list.h:37: error: expected expression before ')' token (type *)((char *) (ptr) - offsetof(type, field_name))) ^

+1

(http://ideone.com/P9phv8)我們可能需要看到您正在使用的宏。 –

+0

宏定義之後是否有分號? –

+0

我沒有看到任何錯誤,嘗試發佈更多的代碼。 –

回答

1

爲錯誤時,最可能的原因,以通過在宏定義,編譯器產生的是,定義過早結束,並且所述第二線被作爲代碼進行編譯。如果在\和行尾之間有額外的空白字符,則會發生這種情況。 或標籤,或者如果文件從Windows環境移動到Linux或Mac系統,可能會出現意外回車。

使宏觀放在一行:[編譯沒有問題]

#define container_of(ptr, t, m) ((t*)((char*)(ptr) - offsetof(t, m)))