简体   繁体   English

指针地址和指针包含的地址之间的差异

[英]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 of a=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. 变量ap只是我们作为程序员构想这些内存位置所需的语法。 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! 在您的情况下,两者都是正确的!

How to understand pointer easily? 如何轻松理解指针?

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. 指针也是一个盒子,除了它包含带有另一个盒子地址的注释。

How you read a 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更改它。

How you read a 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框的地址

Conclusion 结论

  1. Every variable has a memory address, and a value 每个变量都有一个内存地址和一个值
  2. A normal variable just deals with its value directly 一个普通变量只是直接处理其值
  3. A pointer has a value points to another address(most time, the address of another variable). 指针的值指向另一个地址(大多数情况下,是另一个变量的地址)。
  4. The above Box Model also works on int **p (pointer to pointer) and more. 上面的Box Model也可以在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 . 5int变量a
  • &a is the value of int * variable p . &aint *变量p
  • *p is the int value at the address held by int * variable p . *pint *变量p持有的地址处int

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM