我有一个测试结构定义如下:
struct test{
int a, b, c;
bool d, e;
int f;
long g, h;
};
在某处我以这种方式使用它:
test* t = new test; // create the testing struct
int* ptr = (int*) t;
ptr[2] = 15; // directly manipulate the third word
cout << t->c; // look if it really affected the third integer
这在我的 Windows 上正常工作 - 它按预期打印 15,但它安全吗?我真的可以确定变量在我想要的内存中的位置吗?特别是在这种组合结构的情况下(例如 f 在我的编译器上是第五个单词,但它是第六个变量)?
如果没有,是否有任何其他方法可以直接操作 struct 成员,而无需在代码中实际使用 struct->member 构造?