保存数组地址的指针的地址是相同的
How the address of pointer that holds the address of array are same?
这里p
是一个整数指针,它可以保存int
变量的地址,但它也有一个内存地址-它存储在哪里。
让阵列a = 1002
的基址指针p = 2008
地址
当我们写:int *p=a; //p points to the base address of array a
and int **r=&p; //means *r points to the address of p
如果*r
指向a
的地址,那么它应该指向p
的地址。
#include <stdio.h>
void main()
{
int a[3] = {1, 2, 3};
int *p =a;
int **r = &p;
printf("%p %p", *r, a);
}
您的printf
不正确。应该是r
打印r
指向的地址:
printf("%p %p", r, a);
通过使用*r
,您将服从r
(即跳转到r
指向的地址),从而打印a
的地址。
请注意
int *p=a;
表示a
和p
现在指向同一个地址。
同时,r
指向p
(是p
的地址),而不是*r
。
因此,要打印p
的地址,只需在printf()
中使用r
而不是*r
。
printf("%p %p", r, a);
下面是什么?
int **r = &p;
基本上使用以上,r
保存p
的地址对吗?所以如果你像解引用*r
一样解引用r
,它会尝试检索存储在地址p
的值对吗?即a
。
注意:你需要在printf:
中转换为void*
printf("%p %p", (void*)*r, (void*) a);
与->不一样
当你说
int *p = a;
表示p指向a,或者p保存着a的地址
int **r=&p;
R保存p的地址;如果你使用printf("%p %p", r, a);
,你会得到p的地址。
但是由于你对r进行了解引用,所以你得到了a的地址
相关文章:
- C++ 指针的内存地址和指向数组的内存地址如何相同?
- 在C++中打印指向不同基元数据类型的指针的内存地址
- 将地址分配给本地指针后,公共对象的变量将消失
- 为什么指针不写入类的地址?
- CUDA:统一内存和指针地址的更改
- 为什么 vector 的随机访问迭代器给出与指针不同的内存地址?
- 如何从绝对地址的 C 样式指针创建对C++对象的引用
- 当我在 C++ 中将派生类的指针分配给指针时,地址会更改
- 是否可以仅通过将分配的指针地址存储在C++中来分析内存?
- 如何在程序集函数中将元素数组作为参数传递时转发 ARM 寄存器的地址指针
- 如何使用构造函数初始化内存地址(指针变量)?
- 数组基础地址指针
- 如何在不使用返回的情况下从函数获取变量的地址(指针)
- 保存QWidgets的地址/指针
- C++ 读取内存地址/指针和偏移量
- 从存储在 CTreeCtrl 的 LPARAM 中的结构中检索地址/指针 (IXMLDOMNode*) - 不起作用
- 简单的C++地址/指针澄清
- 如何获取成员函数的地址指针
- 当“按引用传递”成为强制传递而不是按地址/指针传递时
- C++ 为什么不是同一个地址(指针)