我嘗試了一個示例程序來初始化如下的結構數組,並且出現錯誤。但是當我用一個變量初始化時,我沒有得到任何東西。初始化一個結構數組
#include<stdio.h>
typedef struct s_{
int a;
int b;
}s;
typedef struct t_{
int c;
int d;
}t;
typedef struct st_{
s s1;
t t1;
}st;
#define ST_ALLOC {{20,20},{10,10}}
int main(){
st str[2] = ST_ALLOC;
//str[0] = ST_ALLOC;
//str[1] = ST_ALLOC;
printf("\n st.s1.a : %d st.t1.d : %d \n",str[1].s1.a,str[1].t1.d);
return 0;
}
錯誤:
a.c: In function ‘main’: a.c:22: error: expected expression before ‘{’ token a.c:23: error: expected expression before ‘{’ token
我不明白爲什麼會這樣。
此代碼爲我編譯。你確定這裏的東西是你想要編譯的嗎? –
這段代碼也是爲我編譯的。 – Ralph
檢查出你的代碼沒有任何修改工作:http://ideone.com/VL9RJ0 – Jack