[英]Difference between the address of a pointer and the address that a pointer contains
#include<stdio.h>
main() {
int a,*p;
a=2;
p = &a;
printf("The address %p contains the address of a=2:%p\n",&p,p);
printf("The address of %d is %p\n",a,&a);
return 0;
}
The result is: 结果是:
The address 0028FF18
contains the address ofa=2: 0028FF1C
The address 0028FF18
包含a=2: 0028FF1C
的地址a=2: 0028FF1C
is this correct? 这个对吗? if not, what's the correct statement??
如果没有,正确的说法是什么?
The address of 2 is 0028FF1C
地址2是0028FF1C
is this correct? 这个对吗? if not, what's the correct statement??
如果没有,正确的说法是什么? */
* /
p有其自己的地址&p,但p的值是a的地址。
Regarding a: 关于:
printf("The value of a is %d.", a); // The value of a is 2.
printf("The address of a is %p.", &a); // The address of a is 0028FF1C
Regarding p: 关于p:
printf("The value of p is the address of a is %p.", p); // The value of p is the address of a is 0028FF1C
printf("The address of p is %p.", &p); // The address of p is 0028FF18
I think of memory on a computer as a street full of addresses (a really long street), and at each address lives a single value. 我认为计算机上的内存是一条充满地址的街道(一条很长的街道),并且每个地址处都有一个值。 The variables
a
and p
are simply syntax needed for us as programmers to conceive of these memory locations. 变量
a
和p
只是我们作为程序员构想这些内存位置所需的语法。 So, a
is a representation for the address 0028FF1C and the current resident of that address is 2 ... in the future, 2 could move out and 4 could move in, but the address would be the same. 因此,
a
是地址0028FF1C的表示形式,该地址的当前居民是2 ...将来,2可以移出而4可以移入,但是地址将相同。
Likewise with p
- his address will always be 0028FF18, and it just so happens that the address of a
is the current resident at this location, but later a different value could move in here (eg, p = &b
) and p
's address would still be 0028FF18. 同样,对于
p
他的地址将始终为0028FF18,恰好碰巧a
的地址是该位置的当前居民,但是稍后可以在其中移动另一个值(例如p = &b
)和p
的地址仍然是0028FF18。
Look at the diagram below: 看下图:
+---------+
| |
| 2 | Value of a=2
| |
+---------+
Address=0028FF1C
+---------+
| |
|0028FF1C | P stores address of a
| | So p=0028FF1C
+---------+
Address=0028FF18
So now your first question is 所以现在你的第一个问题是
1. The address 0028FF18 contains the address of a=2: 0028FF1C. Is this correct?
Answer: Yes, it is correct as 0028FF18 is address of p and it contains &a ie address of a which is 0028FF1C. 答:是的,这是正确的,因为0028FF18是p的地址,并且包含&a,即a的地址是0028FF1C。
Now coming to second question 现在来第二个问题
2. The address of 2 is 0028FF1C. Is this correct?
Answer: This is little bit confusing statement (can't say it's perfectly correct) as address of a is 0028FF1C and value of a is 2. So it might be better if you print it like: 答案:这有点令人困惑的声明(不能说完全正确),因为a的地址是0028FF1C且a的值是2。因此,将其打印如下可能会更好:
printf("The address of a is %p\n",&a);
So it will give output: 因此它将给出输出:
The address of a is 0028FF1C. a的地址是0028FF1C。
This statement seems better as here we are printing address of variable not the address of a value which that variable stores. 该语句似乎更好,因为这里我们打印的是变量的地址,而不是变量存储的值的地址。
A pointer is really just a special kind of number — an address used to locate another piece of data. 指针实际上只是一种特殊的数字-用于定位另一条数据的地址。 In your example,
p
is the numeric address to a
, and &p
is a pointer to the address stored in p
. 在您的示例中,
p
是指向a
的数字地址, &p
是指向存储在p
的地址的指针。 &p
is a pointer to a pointer. &p
是指向指针的指针。
A picture is worth a thousand words, here is what p and a like in memory. 一幅图片值一千个单词,这就是内存中的p和like。
left are address, and right are variable names. 左边是地址,右边是变量名。
+---------------+
0028FF18 | 0028FF1C | p
+---------------+
0028FF1C | 2 | a
+---------------+
In your situation, both are correct! 在您的情况下,两者都是正确的!
Just imagine a variable as a box with label(variable name) on it, and the box itself has an address(memory address) ,such as Room 304 on 3rd floor, for anyone to locate it. 试想一下,变量是一个带有标签(变量名)的盒子,盒子本身有一个地址(内存地址) ,例如三楼的304室,任何人都可以找到它。 In the box lays a note(value) tells you what it is(a
int 3
, a char 's'
, a float 3.14
etc). 在框中放置一个便笺(值),告诉您它是什么(一个
int 3
,一个char 's'
,一个float 3.14
等)。
While a pointer is also a box with the exception that it contains a note with another box's address on it. 指针也是一个盒子,除了它包含带有另一个盒子地址的注释。
int
? int
? How it works on an variable int a=2
? 它如何在变量
int a=2
?
You just say, hey I want to access a
(the box with label a on it), someone(your compiler along with OS) just find the box and give it to you. 您只是说,嘿,我想访问
a
(带有标签a的框),有人(您的编译器以及OS)只要找到该框并将其提供给您即可。 You open the box, and see a note reading 2
(might as well as type int
). 您打开包装盒,看到一个注释为
2
的注释(可能以及输入int
)。 You can change it with another note you just scribbled, say, 3
. 您可以用刚才写的另一个音符(例如
3
更改它。
int *
? int *
? Similarly, say we have int *p = a
. 同样,假设我们有
int *p = a
。 We get the box with label p
on it, open it, and it says Room 305 on 5th floor
. 我们得到带有标签
p
的盒子,将其打开,上面写着Room 305 on 5th floor
。 So instead of saying 'I want the box a
', this time we say 'bring the box in Room 305 on 5th floor'(which is what *p
means). 所以不是说“我想箱子
a
”,这个时候我们说“把框305室5楼”(这是*p
方式)。
*p = 3;
means: bring the box which box p
points to and change its value to 3. p
指向的框,并将其值更改为3。 int *q = &a
: bring a new box(put it somewhere), and fill it with a note reads the address of a
int *q = &a
:带一个新盒子(放到某个地方),并用注解填充,以读取a
&p
: give me the address of box p
&p
:给我p
框的地址 int **p
(pointer to pointer) and more. int **p
(指向指针的指针)上工作。 Maybe a simple analogy will help you understand better. 一个简单的类比也许可以帮助您更好地理解。
int a = 5;
int *p = &a;
5
is the value of int
variable a
. 5
是int
变量a
的值 。 &a
is the value of int *
variable p
. &a
是int *
变量p
的值 。 *p
is the int
value at the address held by int *
variable p
. *p
是int *
变量p
持有的地址处的int
值 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.