Arduino IDE: what is an array or a vector #8

Arduino IDE

Welcome back to our programming tutorial using the Arduino IDE. Today we will deal with arrays: how to declare them, how to use them.

You can take a look at the previous chapters of the course here:

In the previous articles of this guide, simple variables, i.e. variables corresponding to a single value, have been used in the sketches.

However, sometimes we need to aggregate many simple data (variables), in order to facilitate their representation.

This data is organized in a data structure called an array (or vector).

An array can be imagined as a sort of container, whose boxes are called cells (or elements) of the array itself. Each of the cells behaves like a traditional variable; all cells are variables of the same pre-existing type, called the base type of the array. We will therefore talk about types like “integer array”, “string array”, “character array” and so on. What we obtain by declaring this is therefore a static and homogeneous container of values, variables or objects.

The array is a set of homogeneous elements. With a variable we can indicate only one data element, while the array can define a lot of data elements of the same type with a single collective variable name: the identifier of the array. The elements are distinguished from each other through the index that is assigned in the array, and that is placed next to the identifier of the array.

We use the following syntax to declare an array:

The type of components of an array can be any of the standard types of the programming language used by the Arduino IDE.

If a data structure is defined as an array, it must always be used with an integer index. Index numbering starts at 0.

It is also possible to initialize an array by assigning values to the components being declared. The values are indicated inside the braces and separated by a comma.

The ability to access elements through an index is the main feature of an array. In addition, it is possible to scroll through the various values of the vector identified by that specific index by means of an iterative cycle.

 

Arduino IDE array

Once the array has been declared, we can assign data to each corresponding position just using the index of the array.

If we want to insert the value 98 into the int array at position 10, we write:

Note: When recalling the last element of an array, always remember that the counter of the array starts from 0 and not 1. In an array called “numbers”, of 3 elements, and containing the values 8, 3 and 5, you have to write numbers [0] to get the value 8, while to recall 3 you have to write numbers [1].

An array can also be initialised in the definition phase. Of course in such case there is no need to declare the size (number of the elements) of the array, as it will be computer in automatic by the compiler.

The advantage of using a vector instead of a number of different variables:

  • just one simple declaration
  • All the locations of the array can be accessed using a single loop

In the next article we will show some examples to use arrays within the Arduino IDE.

Previous articles:

Follow us to keep yourself updated with the news!

Simone Candido è un ragazzo appassionato del mondo tech nella sua totalità. Simone ama immedesimarsi in nuove esperienze, la sua filosofia si basa sulla irrefrenabile voglia di ampliare a 360° le sue conoscenze abbracciando tutti i campi del sapere, in quanto ritiene che il sapere umano sia il connubio perfetto tra cultura umanistica e scientifica.

Leave a Reply

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