How to Initialize a Dynamic Array in C++? Last Updated : 31 Jan, 2024 Comments Improve Suggest changes Like Article Like Report In C++, dynamic arrays allow users to allocate memory dynamically. They are useful when the size of the array is not known at compile time. In this article, we will look at how to initialize a dynamic array in C++. Initializing Dynamic Arrays in C++The dynamic arrays can be initialized at the time of their declaration by using list initialization as shown: data_type* pointer_variableName = new data_type[ array_size] {element1, element2, ...} ;C++ Program to Initialize Dynamic Arrays C++ // C++ program to demonstrates the initialization of a // dynamic array using a new keyword. #include <iostream> using namespace std; int main() { int size = 5; // initializing a dynamic array int* arr = new int[size]{ 1, 2, 3, 4, 5 }; // printing the array elements cout << "Elements of the array are: " << endl; for (int i = 0; i < size; i++) { cout << arr[i] << " "; } // freeing-up memory space by deleting arr delete[] arr; return 0; } OutputElements of the array are: 1 2 3 4 5 Note: If we create a dynamic array without specifying initial values, the elements will not be initialized, and their values will be undefined. Comment More infoAdvertise with us Next Article How to Initialize a Dynamic Array in C++? V vivekchaudharyy Follow Improve Article Tags : C++ Programs C++ cpp-array Dynamic Memory Allocation C++-new and delete C++ Array Programs CPP Examples +3 More Practice Tags : CPP Similar Reads How to Initialize an Array in C++? In C++, an array is a collection of similar datatypes stored in contiguous memory locations in which each element can be accessed using their indices. In this article, we will learn how to initialize an array in C++. Initializing an Array in C++To initialize an array in C++, we can use the assignmen 2 min read How to Dynamically Resize a C++ Array? In C++, an array is a collection of elements of the same type placed in contiguous memory locations. In this article, we will learn how to dynamically resize an array in C++. Example: Input: myArray = {10, 20, 30, 40, 50};Output:Resized array: 10 20 30 40 50 0 0 0 0 0 Resizing a Dynamic Array in C++ 3 min read How to Initialize a Set with an Array in C++? In C++, an array stores data in contiguous memory locations and can have duplicates as well whereas a set stores unique elements only in a sorted order. In this article, we will learn how to initialize a set with an array in C++. For Example, Input:arr[]={2, 1, 5, 4, 3, 5};Output:Element in Set are: 2 min read How to Initialize Vector of Char Arrays in C++? In C++, a vector is a dynamic array that can grow and shrink in size. A char array is a sequence of characters, typically used to represent strings. In this article, we will learn how to initialize a vector of char arrays in C++. Example: myVector: {âappleâ, âbananaâ, âcherryâ}Initializing a Vector 2 min read How to Print an Array in C++? In C++, an array is a fixed-size linear data structure that stores a collection of elements of the same type in contiguous memory locations. In this article, we will learn how to print an array in C++. For Example, Input: array = {10, 20, 30, 40, 50}Output: Array Elements: 10 20 30 40 50Printing Arr 2 min read Like