所以,我遇到了一些指针问题。 我正在编写一个将内存地址存储在[]中的函数。 这些内存地址指向 b[] 中的实际数据值。 我无法将 b 中的内存地址存储在 a 中。
// Assume these are initialized to 0 outside of this snippet
char a[100];
char b[100];
b[0] = 42; // Some value for us to use
int* a_ptr = (int*)&a[0]; // Store the address of a[0] in a_ptr
int* b_ptr = (int*)&b[0]; // Store the address of b[0] in b_ptr
*a_ptr = (int)&b_ptr; // PROBLEM LINE. The first four bytes of a[]
// should contain the memory address of b[0].
// However, it does not. Here are the debugger values:
// a_ptr = 0x00429148
// b_ptr = 0x00429151
// a[0] SHOULD be 0x00429151, but it is instead 0x0012fe4c.
我需要做一些技巧才能将 0x00429151 存储在 a[0..3] 中吗?
最佳答案
*a_ptr = (int) b_ptr;
根据评论更新
关于c++ - 在数组中获取和存储内存地址的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3518727/