Arrays. Part 1. Array definition. One-dimensional arrays. Initializing array

Arrays. Part 1. Array definition. One-dimensional arrays. Initializing array


1. What is an array? Array definition

Array – a set of variables of the same type. Access to these variables is carried out by the same name. This name is called the name of the array. Arrays are used for grouping related variables between themselves.


2. The definition of one-dimensional and multidimensional arrays.

Arrays can be one-dimentional and multidimentional. In the one-dimensional arrays, only one index is used to access an array element. In multidimensional arrays to access an element of the array are used multiple indexes.


3. Definition of one-dimensional array. Examples of descriptions of one-dimensional arrays

The general form of the description of one-dimensional array:

type array_name[size];

In the definition above:

  • type – the type of array items. It is also called a base type. The base type determines the amount of data of each item that is an array. Array type can be both a base type and the aggregate type (e.g., structure). About the basic C++ types of data is described here in details.
  • size – number of items in array;
  • array_name – directly the array name that provides access to the array elements.

After describing the array, elements value may be zero or undefined.

Example 1. Description of the array of 10 integers (type int) with the name of A.

int A[10];

As a result, 10 int type cells are allocated in the memory of the computer. If one cell is 2 bytes then total will be allocated 20 bytes of memory. Number of the first cell starts from zero. These cells are share a common name of A.

Figure 1. An array of 10 integers.

Example 2. Description array named M, that contains 20 elements of type char.

char M[20];


4. Access to the elements of one-dimensional array. Examples

Access to a single element of the array is implemented by using the index. The index determines the position of the element in the array. The first element of the array has index zero.

To access a separate element of the array by its index, it is necessary after the name of of the array in square brackets specify number of of this element.

Example 1. Let there be given array called A, containing 10 integers. Write the number 5 in the first and last elements of the array.

// definition of the array A
int A[10];

A[0] = 5; // first item
A[9] = 5; // last item

Figure 2 shows the result of the above code.

Figure 2. The result of the code snippet

Example 2. An array of 10 elements of type char.

char M[10];

M[3] = 'a';
M[7] = '0';
M[8] = ';';


Figure 3. An array of 10 elements of type char.

Example 3. An array of 30 reals.

double M[30]; // an array of 30 reals

// zeroing the array
for (int i=0; i<30; i++)
    M[i] = 0.0;


5. How to define the size of a one-dimensional array?

The size of a one-dimensional array is defined by the formula:

array size = the size of type in bytes × number of elements

Example. If the array has 20 elements of type double (8 bytes), the array size will be

size = 20 × 8 = 160 bytes


6. Features of array processing by the compiler in C++. The boundaries of array

In C++ not realized a strict control of access to the elements outside the array. If you defint the array of 100 items, it is possible to read or change the 101 th, 102 th and the following items. In these positions can be memory locations that have been allocated to other variables or even your program. This can lead to the destruction of the program in the absence of any comments from the C++ compiler.

It means all the responsibility for complying the boundaries of arrays lies strictly on the programmer. The programmer must write code that guarantees the correct operation with arrays. This is implemented by including a special checks into the program.

This behavior of the C++ compiler can be explained as follows. The C++ language is designed for professionals. The main purpose of language – give the possibility for the professional programmers create the most efficient code. If in the C++ compiler include the verification code, it would slow down the execution of the program code and will not be as effective as possible.


7. How does the array initialization in C++. Examples

C++ supports two kinds of initializing arrays:

  • initialization using of the size of array;
  • “dimensionless” initialization.

General view of the array initialization with specifying the size:

type array_name[size] = { values_list };


  • type – type of array items;
  • size – the number of elements in the array of the specified type;
  • values_list – a list of values of array initialization. The array elements are separated by ‘ , ‘ (comma).

General view of the “dimensionless” initialization:

type array_name[] = { values_list };

In this case, the array size is determined by the number of elements which are described in values_list.

Example 1. Array B is initialized by the size set.

// initialization of array B
int B[10] = { 5, 6, 9, -8, 3, 2, 4, -90, -103, 0 };

Example 2. Array C is initialized on the basis of a list of values (“dimensionless” initialization).

// "dimensionless" initialization of array C
float C[] = { -3.9, 2.8, -1.6, 2.2 };


8. Initialization of character arrays. Example

For character arrays, you can use a shortened version of the initialization:

char array_name[size] = "string";

In this case, each element of the array is assigned to a single character in the string.

Example. Initialize a character array named str.

// character array - a shortened version of the initialization
char str[] = "Hello!";

// another option of character array initialization
char str2[] = { 'H', 'e', 'l', 'l', 'o', '!' };

In the example above, str and str2 arrays contain the same values


9. The assignment one array to another. Example

In the C++ programming language (unlike other languages) can not directly assign one array to another. You can assign only element-wise with the cycle operator. In this case both arrays must have the same type of elements.

Suppose that two arrays of integers are given. A fragment of code that assigns one array to another:

// definition of arrays A and B
int A[10], B[10];
int i;

//A = B; // error!

for (i=0; i<10; i++)
    B[i] = 0;

for (i=0; i<10; i++)
    A[i] = B[i];


10. Definition of array the structures. Example

Let there be given definition of the structure, which contains information about the book:

struct BOOK
    char title[50];
    char author[50];
    int year;
    float price;

This description defines a new type struct BOOK. In this description, the memory is not allocated. It is only the information about a new type of data. The structure must be described outside the definition of any class at the beginning of the namespace definition.

The array of 5 books (structures) can be described as follows:

struct BOOK Books[5];

Access to the elements of structure in the program:

strcpy(Books[0].title, "Title-1");
strcpy(Books[0].author, "Author-1");
Books[0].year = 1970;
Books[0].price = 28.85;

In the above code is used the function for working with strings

strcpy(str1, str2);

This function copies the string str2 into string str1. To use these and other functions to work with strings at the beginning of the program need to specify a string before the namespace description

#include <cstring>


#include <string.h>

Related topics