2015-11-13 126 views
-2

我的函數有2個結構體作爲輸入。一個在另一個函數中創建,另一個在函數「()」中編寫。但是在編譯時,我得到了錯誤,那些東西預計會變成第二個結構體的{。C:如何聲明匿名結構

static void function() { 
    check(function_two("abcdefg"), {5,2,4,1,1,1}); 
    check(function_two("abcdefg"), {9,3,6,3,1,1}); 
} 
+2

請分享一個完整的運行代碼。 – Haris

+1

和錯誤輸出。 –

+3

什麼是「檢查」?如果它不是宏,那只是一個錯誤的語法。 –

回答

4

如果你想創建一個匿名結構,並且使用的是C99或C的更新版本,您可以使用compound literals。你必須寫:

static void function() { 
    check(function_two("abcdefg"), (MyStruct){5,2,4,1,1,1}); 
    check(function_two("abcdefg"), (MyStruct){9,3,6,3,1,1}); 
} 

其中MyStruct是你的結構的名稱。

+0

然後編譯器在'{' – Alex

+0

之前預期''''您使用了什麼編譯器? – Mabus

+0

使用gcc編譯器進行編譯 – Alex

-1
int check(struct V c, struct E e){ 
if(struct c == struct e){ 
    printiln("Check passed."); 
}else{ 
return 0; 
} 
return 0;} 

struct V{ 
    int a; 
    int b; 
    int c; 
    int d; 
    int e; 
    int f; 
}; 

struct E{ 
    int g; 
    int h; 
    int i; 
    int j; 
    int k; 
    int l; 
}; 

struct V c={0,0,0,0,0,0}; 
struct E e={0,0,0,0,0,0}; 
+0

你能解釋你的答案嗎? –