What is an Array?

This lesson briefly defines arrays, their types, and the difference between one dimensional and two dimensional arrays. We will also discuss how arrays are stored in memory.

In this lesson, we will revise the basic concepts of arrays and go through some practical examples to get a grip over this simple yet powerful data structure.

Introduction #

An array is a collection of items of the same type stored contiguously in memory. It is the simplest and most widely used Data Structure. Most of the other data structures for example stack and queues can be implemented using the array structure. This makes the array one of the central building blocks of all data structures.

Look at the figure below; we have made a simple array with four elements. Each item in the collection is called a Data Element and the number of data elements stored in an array is known as its size.

%0 node_1 0 node_2 1 node_3 2 node_1517899164738 3
Array of length 4

Declaring arrays

In C++, arrays can be declared as either static or dynamic.

Declaring static arrays

In C++, memory allocation for static arrays takes place at compile time. Furthermore, this allocation is on the stack memory.

A generic definition of a static array is given below:

datatype arrayName [size];

Static arrays can be assigned values when they are declared. This is called array initialization. An example of array initialization is:

int dots[3] = { 1, 4, -2 };

When initializing arrays, the size can be ...