简体   繁体   English

指针和指针之间有什么区别

[英]What is the difference between pointer to pointer and a pointer

What is the difference between temp2 and temp3 they both point to head 他们俩都指向的temp2temp3什么区别

node* temp2 = head;
node** temp3 = &head;

What is the difference between pointer to pointer and a pointer 指针和指针之间有什么区别

Actually both pointers are the same: A memory position stored in memory. 实际上,两个指针是相同的:存储在内存中的内存位置。 They only differ in what is stored at the memory position they point to. 它们的区别仅在于它们指向的存储位置中存储的内容。

Memory, addresses and pointers 内存,地址和指针

You can think of computer memory from a programmer view as a list of pairs: 您可以从程序员的角度将计算机内存视为成对的列表:

  • Addresses 地址
  • Values 价值观

Each named object/variable name corresponds to 每个命名的对象/变量name对应于

  • a certain value (accessed via name ) 某个值(通过name访问)
  • at a specific memory address (accessed via &name ) 在特定的内存地址(通过&name访问)

and therefore one of the memory value/address pairs. 因此是内存值/地址对之一。

Example

Let's assume (for simplicity) that node is an integral type. 让我们假设(为简单起见)该node是整数类型。 If we define head with the value 631 : 如果我们用值631定义head

node head = 631; 

A certain memory position (ie 0x002 ) will be chosen (the compiler will choose an offset and the OS will dictate the final position in memory) and the value 631 will be stored at that position. 将选择某个内存位置(即0x002 )(编译器将选择一个偏移量,而OS将指示内存中的最终位置),并且值631将存储在该位置。

----------------------------------
|   Addr.    |    Val   |  Name  |
----------------------------------
|   0x002    |    631   |  head  |
----------------------------------

head is now (and only) an alias or name for the value at a specific memory position ( 0x002 in this example). head现在(并且仅)是特定存储位置(在此示例中为0x002 )的值的别名或名称。

If we define a pointer, there is nothing different happening. 如果我们定义一个指针,没有什么不同。

node* temp2 = &head; // &head == 0x002

Again a memory position is chosen (ie 0x005 ) and the value ( 0x002 ) is stored in that position. 再次选择一个存储位置(即0x005 ),并将值( 0x002 )存储在该位置。

----------------------------------
|   Addr.    |    Val   |  Name  |
----------------------------------
|   0x005    |   0x002  |  temp2 |
----------------------------------

And again the variable name temp2 is only an alias for whatever value is stored in 0x005 . 同样,变量名temp2只是0x005存储的任何值的别名。

The same goes for temp3 again. temp3同样如此。

node** temp3 = &temp2; // &temp2 == 0x002

And the corresponding address/value pair: 以及相应的地址/值对:

----------------------------------
|   Addr.    |    Val   |  Name  |
----------------------------------
|   0x007    |   0x005  |  temp3 |
----------------------------------

The memory layout of this code 此代码的内存布局

node head = 631;
node* temp2 = &head;
node** temp3 = &temp2;

looks like this for the present example: 对于当前示例,如下所示:

在此处输入图片说明

Address-of and dereferencing 地址和取消引用

To turn this into a half-way comprehensive answer with respect to pointers, let's have a quick look at & and * . 要将其转化为关于指针的中途综合答案,让我们快速看一下&*

As I already wrote, each Name stands for a value/address pair. 正如我已经写过的,每个Name代表一个值/地址对。

----------------------------------
|   Addr.    |    Val   |  Name  |
----------------------------------

If you decide to apply & to a certain name, you will get the address of the value/address pair ie: 如果决定将&应用于特定名称,则将获得值/地址对的地址,即:

&temp3 == 0x007

If you apply * instead, this is an alias for whatever is stored at the address corresponding to the current value. 如果您使用*代替,这是存储在与当前值对应的地址上的任何内容的别名。

*temp3 means: " Give me whatever is stored at the address that is stored in the value of temp3 " So we have two steps here: *temp3意思是:“ 给我存储在temp3值中存储的地址中的任何东西 ”,所以我们在这里有两个步骤:

  1. Fetch the address that is stored in the value of temp3 提取存储在temp3值中的temp3
  2. Give whatever is stored at that address 提供存储在该地址的任何内容

Remember: 记得:

----------------------------------
|   Addr.    |    Val   |  Name  |
----------------------------------
|   0x005    |   0x002  |  temp2 |
----------------------------------
|   0x007    |   0x005  |  temp3 |
----------------------------------
  1. The " address that is stored in the value of temp3 " is 0x005 . temp3的值存储的地址 ”为0x005
  2. "Whatever is stored at the address" 0x005 is temp2 . “无论存储在该地址什么” 0x005都是temp2

Therefore 因此

*temp3 == temp2 // temp2 is the dereferenced value of temp3

since 以来

temp3 == &temp2 // value of temp3 is address of temp2

You see: Dereferencing ( * ) is the quasi-opposite to address-of & . 您会看到:取消引用( *&地址近似相反。

Note: * in a declaration declares a pointer and is not the operator that dereferences an address. 注意:声明中的*声明一个指针,而不是取消引用地址的运算符。

A pointer stores a memory location of a data structure, for example your list. 指针存储数据结构(例如您的列表)的存储位置。 A pointer to a pointer will store the memory location of the pointer. 指向指针的指针将存储该指针的存储位置。 It would require an extra dereference to get the the head of the list. 要获得列表的首位,将需要额外的取消引用。 So no temp2 and temp3 do not both point to head. 因此,没有temp2和temp3都不都指向头部。 temp2 points where head points, while temp3 points to the memory location of the head pointer. temp2指向head指向的位置,而temp3指向head指针的存储位置。

Any pointer keeps an address of memory and needs mostly 4bytes (in 32bit systems) of memory to keep this address. 任何指针都保留一个内存地址 ,并且大部分需要4字节(在32位系统中)内存才能保留该地址。 according to this definition we can define a pointer to pointer as following: 根据这个定义,我们可以定义一个指向指针的指针,如下所示:

pointer to pointer : 4 bytes in memory that keeps address of another memory place which it keeps address of somewhere else of memory pointer to pointer :内存中的4个字节,用于保存另一个内存位置的地址 ,该地址用于保存其他内存的地址

Now, We can define temp2 and temp3 as following: 现在,我们可以如下定义temp2和temp3:

temp2 : A place in memory that keeps address of a node object. temp2 :在内存中保留node对象地址的位置。 (mostly every address in a 32bit system need 4 bytes memory ), That this address is equivalence to head object content. (大多数32位系统中的每个地址都需要4字节的内存),该地址等于head对象的内容。

temp3 : A place in memory that keeps address of a pointer to node object in memory, which this pointer to node can be define as a node pointer. temp3 :在内存中的一个位置,用于在内存中保留指向node对象的指针的地址,可以将此指向node指针定义为node指针。
So temp3 keeps address of address of head node, And because of type of head variable is single address then requires & operator that gets address of head variable. 因此,temp3保留头节点地址的地址,并且由于head变量的类型为单个地址,因此需要&运算符来获取head变量的地址。

You can do 你可以做

*temp3 = new_head;

But

*temp2 = new_head;

does not work 不起作用

It is useful for functions eg 对功能有用,例如

void make_list(Node ** head) {
    *head = malloc(sizeof(Node);
} 

Pointer is a variable that holds the address of a variable . 指针是保存变量地址的变量。

We can declare it like : 我们可以这样声明:

char *a;

And then we can assign the address of a variable such as : 然后我们可以分配一个变量的地址,例如:

char b='r';

Like this : 像这样 :

a=&b;

Pointer to a pointer is also a variable which holds the address of a pointer ( the variable that holds the address of any variable ). 指向指针的指针也是一个保存指针地址的变量(该变量保存任何变量的地址)。

We declare it like : 我们这样声明:

char **c;

And then we can assign the address of pointer ie, a like this : 然后我们可以将指针即地址, a是这样的:

c=&a;

Since c contains the address of a which contains the address of b we can later dereference it using a ** 由于c包含a的地址,其中a包含b的地址,我们以后可以使用**取消引用

By this simple example you can understand what do those two statements mean. 通过这个简单的示例,您可以了解这两个语句的含义。 Good luck !! 祝好运 !!

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

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