How to Create an Array in C++


In this article, we show how to create an array in C++.

We show how to declare and initialize an array.

Arrays in C++ are lists that are immutable, which means that no elements can be added or removed from it. Once the array and its elements are created, you can no longer add or remove any elemetns. You can, however, change the values of elements in the array. This is in contrast to a vector array, which is a mutable array, in which elements can be added or removed.

So let's go over now how to declare and initialize arrays in C++.

The general format of declaring and intializing arrays in C++ is shown below.

array_data_type   array_name[number_of_elements_in_array] {value1, value2, ... };

Below are examples of creating arrays in code.

So above we have several different examples so that you can get an idea of different initalizations of arrays in C++.

So the method of declaring an array in C++ is to first declare what data type each item in the array will be. Above some are of type integers (1,2,3,4,5,6 ...) and others are of type double (1.0,2.0,3.0,4.0 ...)

After specifying the data type of the array, you then choose a name for the array. You should make this name descriptive of what the array is for.

After this, you specifiy within parentheses the number of elements in the array. If you specify 5, the array will have 5 elements. If you specify 10, the array will have 10 integers. If you don't specify any number, then array will be the size of the stated initialized values. You see this in the last example, where the compiler will calculate the array size when it is not specified based on how many values you initialize.

After specifying the size of the array, then you initialize the elements of the array. This is done within brackets, {}.

So let's go over each of the examples above now.

So the first array, called test_scores, is an integer array composed of 5 elements. We initialize all 5 elements afterwards.

We then create another integer array that contains 4 elements. However, notice that we only initialize 2 values. When initializing values of an array, you don't have to initialize all of the values of the array. The rest of the items will be automatically initialized to 0. So, in this example, element 0 of the array is 35 and element 1 is 25, while the rest of the items are 0.

We then create an array, named salaries, which is of type double. There are 30 elements in the array and each item is initialized to 0. So each item has a value of 0 for all 30 elements.

In our last array example, we have an array, named prices. We don't explicitly declare the number of elements in the array. However, this is actually ok. Once you initialize the values of the array, the compiler will count the number of items initialized and this will be the size of the array.

And this is how to create an array in C++, declaring and initializing it.

Related Resources

How to Write Contents to a File in C++

How to Append Contents to a File in C++

HTML Comment Box is loading comments...