c++ - 在数组中获取和存储内存地址的问题

标签 c++ c arrays pointers

所以,我遇到了一些指针问题。 我正在编写一个将内存地址存储在[]中的函数。 这些内存地址指向 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/

相关文章:

c - windows下如何拦截C库调用?

c - 32 位 C 内核 - 渲染到 VGA 时奇怪的连续重绘

java - 如何获取字符串数组中的数字并将它们放入 int 数组中

c++ - 生成真正的随机数

c++ - D3D9 2D 动画中的奇怪之处——高 FPS 但仍然不流畅

c - 将值附加到 c 数组

javascript - 循环遍历两个下拉选项以填充文本框

c - 段错误 malloc 指针函数

c++ - 第一个字母大写,其余字母小写

c# - 从 MFC 对话框单位转换为像素