我将一个结构的内存位置存储为一个整数。如何检索存储在该位置的结构并指向该位置的对象?
Structure object;
int memLocation = &object;
Structure objectCopy = (objectAtLocation) memLocation;
Structure *objectPointer = (pointerToLocation) memLocation;
最佳答案
使用 int
代替指针是非常糟糕的形式,甚至可以追溯到 K&R 的第一版。所以你在做什么是不好的。但假设您别无选择...
如果 Object
是一个 Structure
,那么 &object
就是一个 Structure *
。所以正确的 un-cast 基本上是你的第 3 行:
Structure *objectPointer = (Structure *) memLocation;
关于c - 如何从特定内存位置获取结构?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9569627/