如何从特定的内存位置获取结构?

Sta*_*aro 1 c memory pointers

我将结构的内存位置存储为整数.如何检索存储在该位置的结构并在该位置创建指向该对象的指针?

Structure object;
int memLocation = &object;

Structure objectCopy = (objectAtLocation) memLocation;
Structure *objectPointer = (pointerToLocation) memLocation;
Run Code Online (Sandbox Code Playgroud)

jap*_*iss 6

使用ints而不是指针是非常糟糕的形式,甚至回到第一版K&R.所以你正在做的事情很糟糕.但假设你别无选择......

如果ObjectStructure,那么&objectStructure *.所以适当的拆解基本上就是你的第3行:

Structure *objectPointer = (Structure *) memLocation;
Run Code Online (Sandbox Code Playgroud)