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
你可以省略它。 –
但不要返回堆栈指针。它不会做你打算做的事情。 –