Arduino – Array

An array is a memory location that stores the same type of elements. As a reference to a specific location or element in the array, enter the name of the array and the position number of that element in the array.

Declaring arrays

Any element in an array can be referenced by specifying the position number of that element in square brackets [] next to the array name. The position number is called the index, and this number determines the position of the element from the beginning of the array. The index of the first element is 0 (zero). When declaring an array of type char, an element is required for initialization, and the required null character indicates the end of the array.

Arrays take up space in memory. The type of array and the number of its elements determine the amount of memory allocated. When declaring arrays, pay close attention to ensure that the array size is sufficient for the elements to be stored in it. The compiler allocates the appropriate amount of memory. The following example allocates enough memory space for a 4-element array named myIntArray.

type arrayName[arraySize];
int myIntArray[4]; // 4-element int type array

The size of the array (arraySize) is a constant integer greater than zero.

const int arraySize = 4;
int myIntArray[arraySize];

You can also enter values at the same time as declaring. In this example, we introduce an array of type int called myIntArray and populate it with 4 elements, in this case a number. Thus also allocates the appropriate amount of memory.

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

Operations with arrays

The elements of the array can be referenced by their index. For example, the third element of the myIntArray array is found in index 2.

int myIntArray[] = {1,2,3,4};
Serial.println(myIntArray[2]);

It displays on the console: 3

You can print all the elements of an array with a for loop.

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

for ( int i = 0; i < 4; ++i )
{
  Serial.print(myIntArray[i]);
  Serial.print (", ") ;
}

The output will be 1, 2, 3, 4,

You can also perform operations on elements of an array.

int myIntArray[] = {1,2,3,4};
Serial.println(myIntArray[0] + myIntArray[2]);
Serial.println(myIntArray[3] - myIntArray[1]);
Serial.println(myIntArray[3] * myIntArray[1]);
Serial.println(myIntArray[3] / myIntArray[1]);
Serial.println(myIntArray[3] % myIntArray[2]);

But we can also use a loop.

int myIntArray[] = {1,2,3,4};
int sum = 0;

for ( int i = 0; i < 4; ++i )
{
  sum += myIntArray[i];
}

Serial.println(sum);

The result will be 10 on the console.

Multidimensional arrays

Two- or multi-dimensional arrays often store table values ​​that consist of information arranged in rows and columns. To identify a particular table element, we need to specify two indexes. The first identifies the row of the element and the second identifies the column of the element. Arrays that require two indexes to identify a given element are called two-dimensional arrays or 2-D arrays. Arrays of two or more dimensions can also be called multidimensional arrays.

Let’s see an example.

const int rows = 2;
const int cols = 3;

int myIntArray[rows][cols] = {
  {1,2,3},
  {4,5,6}
};
myIntArray [0] [0] ==> 1myIntArray [0] [1] ==> 2myIntArray [0] [2] ==> 3
myIntArray [1] [0] ==> 4myIntArray [1] [1] ==> 5myIntArray [1] [2] ==> 6
2-dimensional array

You can create a string with a string array. These can be declared according to the following examples, all of which are valid. The last element of the character arrays is 0 (zero), hence the compiler recognizes the end of the string.

char Str1[15];
char Str2[8] = {'a', 'r', 'd', 'u', 'i', 'n', 'o'};
char Str3[8] = {'a', 'r', 'd', 'u', 'i', 'n', 'o', '
char Str1[15];
char Str2[8] = {'a', 'r', 'd', 'u', 'i', 'n', 'o'};
char Str3[8] = {'a', 'r', 'd', 'u', 'i', 'n', 'o', '\0'};
char Str4[ ] = "arduino";
char Str5[8] = "arduino";
char Str6[15] = "arduino";
'}; char Str4[ ] = "arduino"; char Str5[8] = "arduino"; char Str6[15] = "arduino";

Characters can be written as apostrophes in arrays.

char myArray[2][5] = { 
  {'A','B','C','D'},
  {'1','2','3','4'} 
};

Strings must be enclosed in quotation marks.

const int rows = 4;
const int cols = 5;

char myCharArray[rows][cols] = {
  "row1",
  "row2",
  "row3", 
  "row4"
};

void setup()
{
  Serial.begin(115200);
  Serial.println(myCharArray[3][3]);   // console => 4
}

void loop(){}

If you enter a string in the array, you can reference its characters with their index. With the for loop we can print the contents of the array to the console.

const int rows = 4;
const int cols = 5;
char myCharArray[rows][cols] = {
  "row1",
  "row2",
  "row3",
  "row4"
};
void setup()
{
  Serial.begin(115200);
  for ( int i = 0; i < 4; ++i )
  {
    for (int j = 0; j < 4; j++)
    {
      Serial.print(myCharArray[i][j]);
    }
  }
}
void loop(){}

You can also store longer texts in an array.

char myString[] = 
   "First line"
   "Second line"
   "Third";

Serial.println (myString[7]);

So much for the blocks for now. Scroll on and learn a few more interesting things about strings.