C++ Pointer To Pointer

Hi everyone, inside this article we will see about C++ Pointer To Pointer.

In C++, a pointer to a pointer (also known as a “double pointer“) is a pointer variable that stores the memory address of another pointer variable. The pointer to a pointer is used to indirectly access a memory location that is pointed to by a pointer.

Here is an example program that demonstrates how to use a pointer to a pointer in C++:

#include <iostream>

using namespace std;

int main() {
  int value = 10;        // An integer value

  int *ptr1 = &value;    // A pointer to an integer

  int **ptr2 = &ptr1;    // A pointer to a pointer to an integer

  cout << "The value of the integer is " << **ptr2 << endl;  // Indirectly access the integer

  return 0;
}

The output of this program will be:

The value of the integer is 10

In this program, we first declare an integer variable value, and then create a pointer ptr1 that points to the memory location of value. We then create a pointer ptr2 that points to the memory location of ptr1.

To indirectly access the value of value through ptr2, we use the double dereference operator **. This operator first dereferences ptr2 to obtain the value of ptr1, and then dereferences ptr1 to obtain the value of value.

It is important to note that the syntax for using a pointer to a pointer can be somewhat confusing, and it can be easy to make mistakes when handling multiple levels of indirection. It is important to keep track of the levels of indirection, and to ensure that the pointers are initialized and dereferenced correctly.

We hope this article helped you to understand about C++ Pointer To Pointer in a very detailed way.

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.