結構考慮:構造在C
struct objStruct {
int id;
int value;
};
typedef struct objStruct Object;
是否有一個快捷方式來分配和初始化對象,有點像C++的構造函數?
它甚至可能是一個預處理宏。無論什麼使代碼更短,更易讀:
Object *newObj = malloc(sizeof(Object));
// successful allocation test snipped
newObj->id = id++;
newObj->value = myValue;
[C中的構造函數]的副本(http://stackoverflow.com/questions/537244/constructor-in-c),[初始化C結構](http://stackoverflow.com/questions/1994841/initialising- c結構) – outis 2010-09-22 23:28:45
沒有可能的重複項([C中的默認構造函數](http://stackoverflow.com/questions/537244/)或[在C++代碼中初始化C結構](http://stackoverflow.com/questions/1994841 /))對此非常重要,儘管它們是相關的。自上次發表評論以來,它們已被重新命名。 – 2014-12-22 18:50:00