0
假設我有以下代碼:存儲結構對象的空指針
struct inst {
uint32_t field1;
uint16_t field2;
void *priv;
};
struct ops {
int (*init)(void);
int (*deinit)(void);
int (*run)(void);
};
因此我可以這樣做:
struct inst p1;
struct ops ops;
/* init p1 and ops.*/
...
inst->priv = ops;
它是安全的這樣以後訪問priv
數據:
return (struct ops *)inst->priv
感謝您的意見。我知道關於返回堆棧分配對象的問題,而我的問題是關於'返回(struct ops *)inst-> priv'中的類型轉換 - 是需要還是可以省略? – Mark
你可以省略它。 –
但不要返回堆棧指針。它不會做你打算做的事情。 –