C++ Pointer To Pointer (Double Pointer)

0
16


In C++ a Pointer is a variable that’s used to retailer the reminiscence tackle of different variables. It’s a  variable that factors to an information sort (like int or string) of the identical sort and is created with the * operator.

Syntax of a Pointer in C++:

data_type_of_pointer *name_of_variable = & normal_variable;

What’s a Pointer to a Pointer or Double Pointer in C++?

Now, we already know {that a} pointer shops the reminiscence tackle of different variables. So, after we outline a pointer to a pointer, the primary pointer is used to retailer the tackle of the variables, and the second pointer shops the tackle of the primary pointer. For this very cause, this is named a Double Pointer or Pointer to Pointer.

The beneath diagram explains the idea of Double Pointers

Double Pointer

 

The above diagram reveals the reminiscence illustration of a Pointer to Pointer or a Double Pointer, we will simply perceive that the tackle of the variable (i.e Deal with 1) is saved in Pointer 1 and the tackle of Pointer 1(i.e Deal with 2) is saved in Pointer 2. This is named Double Pointers or Pointer to Pointer.

Easy methods to Declare a Pointer to a Pointer in C ++?

Declaring a Pointer to Pointer is much like declaring a pointer in C++. The distinction is we’ve got to make use of a further * operator earlier than the title of a Pointer in C++.

Syntax of a Pointer to Pointer(Double Pointer) in C++:

data_type_of_pointer **name_of_variable = & normal_pointer_variable;

Instance:

int val = 169;

int *ptr = &val; // storing tackle of val to pointer ptr.

int **double_ptr = &ptr; // pointer to a pointer declared which is pointing to an integer.

The beneath diagram explains the idea of Double Pointers: 

How does Double Pointer works?

 

The above diagram reveals the reminiscence illustration of a pointer to a pointer. The primary pointer ptr1 shops the tackle of the variable and the second pointer ptr2 shops the tackle of the primary pointer. 

Example: How does Double Pointer works?

 

Under is the C++ Program to implement Pointer to Pointer:

C++

#embrace <bits/stdc++.h>

utilizing namespace std;

  

int most important()

{

  int variable = 169;

    

  

  

  int* pointer1;

  

  

  

  int** pointer2;

  

  

  

  pointer1 = &variable;

  

  

  

  pointer2 = &pointer1;

  

  

  

  

  cout << "Worth of variable :- " << 

           variable << "n";

  cout << "Worth of variable utilizing single pointer :- " << 

           *pointer1 << "n";

  cout << "Worth of variable utilizing double pointer :- " << 

           **pointer2 << "n";

  return 0;

}

Output

Worth of variable :- 169
Worth of variable utilizing single pointer :- 169
Worth of variable utilizing double pointer :- 169

What would be the measurement of a pointer to a pointer in C++?

Within the C++ programming language double pointer behave equally to a traditional pointer. So, the dimensions of the variable of the double-pointer and the dimensions of the conventional pointer variable is all the time equal.

Under is a C++ program to verify the dimensions of a double pointer:

C++

#embrace <bits/stdc++.h>

utilizing namespace std;

  

int most important()

{

  int val = 169;

  int* ptr = &val;

  int** double_ptr = &ptr;

  

  cout << " Dimension of regular Pointer: " << 

            sizeof(ptr) << "n";

  cout << " Dimension of double Pointer: " << 

            sizeof(double_ptr) << "n";

  return 0;

}

Output

 Dimension of regular Pointer: 8
 Dimension of double Pointer: 8

Observe: The output of the above code additionally will depend on the kind of machine which is getting used. The dimensions of a pointer isn’t mounted within the C++ programming language and it completely will depend on different components like CPU structure and OS used. Often, for a 64-bit Working System, a measurement of 8 bytes reminiscence and for a 32-bit Working system, a measurement of 4 bytes reminiscence is assigned.

LEAVE A REPLY

Please enter your comment!
Please enter your name here