定義結構體後總體賦值時發生錯誤express
typedef struct NODE { struct NODE *fwd; struct NODE *bwd; int value; } Node; //聲明變量 Node first; first = {NULL, &second, 4};
錯誤: error: expected expressionspa
之因此不行是,{}內容被當作語句塊了,c語言容許直接對結構體初始化,或者在賦值的時候加上類型說明,或者是逐一賦值。blog
初始化賦值: get
typedef struct NODE { int value } Node; //初始化賦值 Node a = {1}; Node b; b = a;
添加類型賦值:io
Node A; A = (Node){NULL, NULL, 1};
單個賦值:class
Node A; A.value = 1;
因此,除非初始化或者強制類型轉換不然,沒法總體賦值結構體。變量
http://stackoverflow.com/questions/12189480/why-structs-cannot-be-assigned-directlyerror