前几天我有这个结构:
struct foo_t {
char a, b, c;
} *foo = (foo_t*)untyped_memory;
...但是为它命名类型太过分了。但是,它的未命名形式:
struct {
char a, b, c;
} *bar = untyped_memory;
...由于指针类型不兼容而未编译。
有什么办法可以让它发挥作用吗?
最佳答案
如果您可以访问 C++11 或更高版本,那么您可以使用 decltype
即
struct {
char a, b, c;
} *bar = (decltype(bar))untyped_memory;
关于c++ - 如何将指针转换为未命名的结构指针?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52129910/