我想了解下面的編譯錯誤(使用gcc)背後的原因。C - 初始化後填充struct - 編譯錯誤
0. struct mystruct {
1. int x;
2. int y;
3. };
4.
5. int foo() {
6. struct mystruct m = {1}; // compiles successfully
7. m = {2,3}; // compilation error: expected expression before ‘{’ token
8. return m.x + m.y;
9. }
但是,如果我在第7行顯式轉換值,代碼編譯:
5. int foo() {
6. struct mystruct m = {1}; // compiles successfully
7. m = (struct mystruct){2,3}; // compiles successfully
8. return m.x + m.y;
9. }
我想了解這個錯誤背後的原因。 第6行成功編譯而不會引發錯誤 - 編譯器會自動計算出m
的類型,而無需進行明確的轉換。爲什麼它在第7行沒有這樣做?
謝謝
解決爲什麼它是必要的:編譯器應如何知道初始化之外的{2,3}類型?可能是'char [2]'以及'struct {int a,b; }'etc. – mafso 2014-09-23 14:24:24
它可以像第6行一樣完成 - 使用'm'類型。 – Oren 2014-09-23 14:50:36
這將與整個語言的其他部分不一致。 C沒有類型推斷,每個表達式都有一個類型。 「{2,3}」不能是表達式,因爲它沒有類型。這些規則在允許的地方會非常複雜,並且會使語言不那麼強大。例如,你可以使用複合文字來做'void * bar =&(struct mystruct){2,3};'等等。 – mafso 2014-09-23 15:11:45