c++ - 为什么指针显示的是值而不是地址?
问题描述
我有这个代码:
int *array_ptr{nullptr};
array_ptr = new int[10]; // allocate array on the heap
for(int i=0; i<10; i++){
array_ptr[i] = i;
std::cout<<array_ptr[i]<<std::endl;
}
delete [] array_ptr; // deletes allocated space
据我了解,int *array_ptr
显示实际值,而int array_ptr
没有取消引用运算符则显示内存中的地址。
那么,为什么array_ptr[i]
显示0 1 2 3 4 ... 10,而不是显示地址1000、1004、1008...等。(地址编造)。
解决方案
指针的意思是“指示地址中有哪个值?”
如果要查看变量的寻址,则必须使用 & 运算符。
在 for 循环中试试这个
std::cout << &array_ptr[i] << std::endl;
推荐阅读
- swiper - SwiperJS Swiper Slider 在缩放时禁用滑动,但允许单击缩略图
- docker - 使用 Ansible 重启多个 Docker 容器
- android - 当我删除项目时,Recyclerview 没有更新
- python - ImportError:DLL 加载失败,pybind11 和 PCL
- python-3.x - 数组的双曲正弦和余弦
- javascript - 如何在javascript中以度数显示风向?
- javascript - 无法从节点模块包加载 css
- r - 有没有办法根据文件的存在而不是特定时间来触发 cron 作业运行?
- javascript - 是否有 JavaScript 代码自动点击下一步
- android - Android中“.mk”文件的printf等价物是什么