Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Pointer to Pointer Dynamic Allocation | Dynamic Memory Allocation
C++ Pointers and References
course content

Course Content

C++ Pointers and References

C++ Pointers and References

1. Pointers Fundamentals
2. Pointer Arithmetic
3. References Fundamentals
4. Dynamic Memory Allocation

bookPointer to Pointer Dynamic Allocation

A pointer to pointer, denoted as double pointer (**).

This is a pointer that holds the memory address of another pointer. In simple words, it is a variable whose value is the address of another pointer. This concept might sound complex at first, but it provides a powerful mechanism for dealing with advanced dynamic memory allocation.

Syntax

cpp

main

copy
12345678910111213
#include <iostream> int main() { int x = 10; int *ptr1 = &x; int **ptr2 = &ptr1; // Accessing values using double pointer std::cout << "Location of ptr2: " << ptr2 << std::endl; std::cout << "Location of ptr1: " << *ptr2 << std::endl; std::cout << "Value of x: " << **ptr2 << std::endl; }
  • ptr1: is a pointer to an integer (int*);
  • ptr2: is a double pointer to an integer (int**).

Dynamic Allocation of a Two-Dimensional Array

If you want to create a two-dimensional array dynamically (at runtime) you have to use a pointer to a pointer for the rows.

And then initialize each row with dynamic array (like in previous chapter)

Task

  • Dynamically allocate a two dimensional array.
  • Dynamically allocate the arrays as elements.
  • Free all allocated memory.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 4. Chapter 4
toggle bottom row

bookPointer to Pointer Dynamic Allocation

A pointer to pointer, denoted as double pointer (**).

This is a pointer that holds the memory address of another pointer. In simple words, it is a variable whose value is the address of another pointer. This concept might sound complex at first, but it provides a powerful mechanism for dealing with advanced dynamic memory allocation.

Syntax

cpp

main

copy
12345678910111213
#include <iostream> int main() { int x = 10; int *ptr1 = &x; int **ptr2 = &ptr1; // Accessing values using double pointer std::cout << "Location of ptr2: " << ptr2 << std::endl; std::cout << "Location of ptr1: " << *ptr2 << std::endl; std::cout << "Value of x: " << **ptr2 << std::endl; }
  • ptr1: is a pointer to an integer (int*);
  • ptr2: is a double pointer to an integer (int**).

Dynamic Allocation of a Two-Dimensional Array

If you want to create a two-dimensional array dynamically (at runtime) you have to use a pointer to a pointer for the rows.

And then initialize each row with dynamic array (like in previous chapter)

Task

  • Dynamically allocate a two dimensional array.
  • Dynamically allocate the arrays as elements.
  • Free all allocated memory.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 4. Chapter 4
toggle bottom row

bookPointer to Pointer Dynamic Allocation

A pointer to pointer, denoted as double pointer (**).

This is a pointer that holds the memory address of another pointer. In simple words, it is a variable whose value is the address of another pointer. This concept might sound complex at first, but it provides a powerful mechanism for dealing with advanced dynamic memory allocation.

Syntax

cpp

main

copy
12345678910111213
#include <iostream> int main() { int x = 10; int *ptr1 = &x; int **ptr2 = &ptr1; // Accessing values using double pointer std::cout << "Location of ptr2: " << ptr2 << std::endl; std::cout << "Location of ptr1: " << *ptr2 << std::endl; std::cout << "Value of x: " << **ptr2 << std::endl; }
  • ptr1: is a pointer to an integer (int*);
  • ptr2: is a double pointer to an integer (int**).

Dynamic Allocation of a Two-Dimensional Array

If you want to create a two-dimensional array dynamically (at runtime) you have to use a pointer to a pointer for the rows.

And then initialize each row with dynamic array (like in previous chapter)

Task

  • Dynamically allocate a two dimensional array.
  • Dynamically allocate the arrays as elements.
  • Free all allocated memory.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

A pointer to pointer, denoted as double pointer (**).

This is a pointer that holds the memory address of another pointer. In simple words, it is a variable whose value is the address of another pointer. This concept might sound complex at first, but it provides a powerful mechanism for dealing with advanced dynamic memory allocation.

Syntax

cpp

main

copy
12345678910111213
#include <iostream> int main() { int x = 10; int *ptr1 = &x; int **ptr2 = &ptr1; // Accessing values using double pointer std::cout << "Location of ptr2: " << ptr2 << std::endl; std::cout << "Location of ptr1: " << *ptr2 << std::endl; std::cout << "Value of x: " << **ptr2 << std::endl; }
  • ptr1: is a pointer to an integer (int*);
  • ptr2: is a double pointer to an integer (int**).

Dynamic Allocation of a Two-Dimensional Array

If you want to create a two-dimensional array dynamically (at runtime) you have to use a pointer to a pointer for the rows.

And then initialize each row with dynamic array (like in previous chapter)

Task

  • Dynamically allocate a two dimensional array.
  • Dynamically allocate the arrays as elements.
  • Free all allocated memory.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Section 4. Chapter 4
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
some-alt