How to declare and initialize an array in C++?

In this article, we will discuss different ways to declare and initialize an array in C++.

Table Of Contents

How to declare an array in C++?

In C++, array declaration requires defining the type of array and the size of the array, i.e., the number of elements to be stored in an array.

The Syntax for the declaration of the array is:

data_type array_name [array_size] ;
  • data_type : The data_type refers to the type of elements that are stored in an array.
  • array_name : The array_name is the name that we assign to the array.
  • array_size : The array_size is the size of the array. It tells us the number of elements that are stored in the array.

Allocate memory on Stack

Advertisements

We cannot declare the array without specifying the size of the array. So, specifying the size of the array is a must, while declaring the array. For example, here we can declare an array of type int, array_name as num and the array_size is 10

int num[10];

It will allocate stack memory, and the scope of this memory is confined to the scope of the function in which the array is declared. When we end the function, this memory will be automatically released/freed.

Allocate memory on Heap

The new operator in C++ can be used to build a dynamic array. The memory for the array is allocated on the heap at runtime with the new operator. The following code, will build a dynamic integer array of size 10 on the heap. In this we have to manually released/freed the memory space using the delete operator.

int *num = new int[10];

How to Initialize an array in C++?

The process of allocating/storing elements to an array is known as array initialization. The initialization can be done all at once or one at a time. It’s worth noting that the first element in an array is kept at index 0, and the last member is stored at index n-1, where n is the total number of elements in an array.

In our above example, the first element will be stored at 0th index and the last element will be stored at 9th index, as the size of array is 10.

Array Initialization by already declared array of specified size

Here in this way, one thing we have to note that the total number of elements within the curly brackets {} cannot exceed the size of the array i.e, the number stated within the square brackets [].

int num[10] = {10,20,30,40,50,60,70,80,90,100};

Array Initialization by initializing elements

In this method, we can initialize the array by assigning elements to the array directly, without specifying the size. If we don’t specify the number of elements to be stored in the array within the square brackets [], the array will be large enough to hold the elements added within curly brackets { }.

int num[] = {10,20,30,40,50,60,70,80,90,100};

User defined array declaration and array initialization

In this method, the user declares the array by taking input the size of the array and then initialize the array by taking input of elements.

#include <iostream>
using namespace std;

int main()
{
    // taking size of the array
    int n;
    cin >> n;

    // declared user defined array
    int num[n];

    for (int i = 0; i < n; i++)
    {
        // initialize the user defined array 
        // by taking elements as input
        cin >> num[i];
    }

    // Output
     for (int i = 0; i < n; i++)
    {
        cout << num[i]<<" ";
    }
    return 0;
}

Input

10

10 20 30 40 50 60 70 80 90 100

Output

10 20 30 40 50 60 70 80 90 100

Summary

In this article, we have seen various ways to declare and initialize the array, and at last we have also discussed and coded the user defined declared and initialized array.

Do you want to Learn Modern C++ from best?

We have curated a list of Best C++ Courses, that will teach you the cutting edge Modern C++ from the absolute beginning to advanced level. It will also introduce to you the word of Smart Pointers, Move semantics, Rvalue, Lambda function, auto, Variadic template, range based for loops, Multi-threading and many other latest features of C++ i.e. from C++11 to C++20.

Check Detailed Reviews of Best Modern C++ Courses

Remember, C++ requires a lot of patience, persistence, and practice. So, start learning today.

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top