What is an Array Data Structure? (With explanation in C++)

What is an Array Data Structure? (With explanation in C++)

An array data structure is a collection of basically similar data types (integers, characters or etc.) which is labelled under the same name. The basic thing which is important about the arrays is that the elements of arrays are stored linearly or one after another in memory.

Each element of an array is denoted by an index or a subscript. The index is usually a number which tells the address of an element in an array. For example; if you were storing information about each hour in a day, you would create an array with an index capable of addressing 24 values (one index for each hour of a day). Indexing can start from 0 or 1 depending on the language you use. The initial index value for an array in C++ is 0.

A Simple Array

How can we declare an array in C++?

In order to declare an array in C++, the programmer needs to specify the type of the elements in the array and the number of the elements as below:

In the above syntax, array-size can be any integer number greater than zero and data-type can be any valid data type in C++.

For example; in order to declare an array named age which will store the ages of 23 students (data-type is integer) in a class you need to follow the following syntax:

Note: Like any other variables in C++, array has to be declared before it is used in the code.

See Also: 

How can we initialize an array in C++?

Suppose I have created an array as the following;

I can initialize the values stored in the array one by one or all at once. Both the ways are explained below:

These statements are both the same. They both initialize the value of the array at the time of their declaration. In case I do not initialize the values of an array at the time of the declaration, they will be assigned the default fundamental value (in some cases the value 0).

OR; you can initialize the values of the array one by one as below:

Note: The initial value of the index in an array in C++ is 0. Therefore; the index value 4th will be the 5th value in the array.

It is also possible to declare an array without the array-size and initialize its values. In this case the compiler itself will create an array equal to the number of values you have initialized in the array. As below:

For the above statement, an array size 5 will be created by the compiler.

See Also: 

How can we access the elements of an array in C++?

You can simply access the elements of an array by indexing the array name.

Suppose you have an array;

And you are going to assign the 6th element of the array to the variable student1;

Why 5??? Because the indexing of arrays in C++ starts from 0. So the index value 5th will demonstrate the 6th element in the array.

Below you can find an array program written in C++;

Array Program in C++

and there you go “The OutPut of the Program”!

Array Output

Some characteristics of arrays are:

  1. All the elements of an array share the same name and they are differentiated from each other by the numbers called index or subscript.
  2. Any element of an array can be called and modified without disturbing the other elements.
  3. All the array elements are stored in linear or one after another memory locations.
  4. An element of an array can be assigned to another variable anywhere in the code.

See Also: 

Note: There are further discussions on arrays, such as Operations of Arrays and types of Arrays. They will be discussed in the upcoming articles on 

Previous How to Group Related Values of Different Data-Types in C++
Next How to Make Series of Prime Numbers in C++

About author

Bahonar Javid
Bahonar Javid 43 posts

<p>* Computer Application Student<br /> * Co-Founder of www.codejow.com<br /> * Love to code and Learn new languages<br /> * Enjoy playing football</p>

View all posts by this author →

You might also like