简体   繁体   中英

What is the difference between pointer to pointer and a pointer

What is the difference between temp2 and temp3 they both point to head

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

  • a certain value (accessed via name )
  • at a specific memory address (accessed via &name )

and therefore one of the memory value/address pairs.

Example

Let's assume (for simplicity) that node is an integral type. If we define head with the value 631 :

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.

----------------------------------
|   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).

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.

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

And again the variable name temp2 is only an alias for whatever value is stored in 0x005 .

The same goes for temp3 again.

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.

----------------------------------
|   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:

  1. Fetch the address that is stored in the value of 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 .
  2. "Whatever is stored at the address" 0x005 is 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 points where head points, while temp3 points to the memory location of the head pointer.

Any pointer keeps an address of memory and needs mostly 4bytes (in 32bit systems) of memory to keep this address. 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

Now, We can define temp2 and temp3 as following:

temp2 : A place in memory that keeps address of a node object. (mostly every address in a 32bit system need 4 bytes memory ), That this address is equivalence to head object content.

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.
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.

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 :

c=&a;

Since c contains the address of a which contains the address of b we can later dereference it using a **

By this simple example you can understand what do those two statements mean. Good luck !!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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