Arduino Array Length: How To Use the Sizeof Function

In this article, we will look at how to get the Arduino array length when dealing with this data structure in your project. Arrays are the most common and simplest data structures used in programming, …

In this article, we will look at how to get the Arduino array length when dealing with this data structure in your project.

Arrays are the most common and simplest data structures used in programming, and you’ll most likely have to implement them in your Arduino board project.

While using arrays, you might have to get the number of elements stored inside to help in the logic flow of your code.

We will show you how to get this data using the size of the function and talk about other vital details about arrays in Arduino. Take a look!

Table of Contents

What Is an Array?

An array is a simple data structure that stores indexed variables in contiguous memory locations, and these data elements must be similar.

How To Declare an Array in Arduino

You can create arrays using these three methods.

  • int myarray[3];
  • int myarray[3] = {4, 6, 7};
  • int myarray[] = {3, 7, 8, 13, 56};

The first and second methods explicitly define the array length in the square brackets.

But the third method shows it is possible to declare an array without indicating its size explicitly.

In this case, the compiler will count the elements stored in the data structure, then create the appropriate size.

Remember, each array holds indexed variables of the same data type. So you can declare it as a float, char, or string array.

C/C++ code with an array data structure

C/C++ code with an array data structure

How To Check the Arduino Array Length

After declaring an Arduino array, you can check its length using the function size. However, its use isn’t as straightforward as declaring the function and passing the array as the argument.

Size returns the number of bytes the array’s elements consume in memory.

So it can take in an input variable of any data type, then give the number of bytes the variable occupies.

Therefore, to get an array’s length, you must first get the number of bytes the array’s elements take up in memory.

After that, you divide this value by the number of bytes of the array’s data type.

Consider the following code.

void loop() {

int samplearray[3] = {4, 6, 8};

int array length = size of(sample array) / size of(int);

Serial.print ln(array length);

}

The alternative is to use the size of the function with a single array element in the argument.

Here’s an example.

void loop() {

int samplearray[3] = {4, 6, 8};

int array length = size of(sample array) / size of(samplearray[0]);

Serial.print ln(array length);

}

Other Data Types

When dealing with arrays holding other data types, you only have to change the argument in the second size of the function to match the array’s data type to get the correct array length. Let’s look at float and char arrays.

Float

void loop() {

float samplearray[3] = {4.5, 6.7, 8.98};

int array length = size of(sample array) / size of(float);

Serial.print ln(array length);

}

Note we have replaced int with float when dividing the bytes in the size of() functions, and you can also use the following code.

void loop() {

float samplearray[3] = {4.5, 6.7, 8.98};

int array length = size of(sample array) / size of(samplearray[0]);

Serial.print ln(array length);

}

A programmer at work

A programmer at work

Character

You can use either of the following lines of code to check the length of a character array.

void loop() {

char c[] = “Hello”;

int arraylength= size of(c) / size of(char);

Serial.print ln(array length);

}

or

void loop() {

char c[] = “Hello”;

int arraylength= size of(c) / size of(c[0]);

Serial.print ln(array length);

}

With a character array, the array length will always have an extra element.

For instance, the output on the serial monitor for the code above will be 6, but “Hello” only has five characters. Why?

Char arrays have an extra null character added at the end, which increases the array’s length by one.

So you can deduct one from the result to get the correct length.

How To Access an Array in Arduino

Variables in an array have indexes that begin from zero.

So the first element always has the index 0, while the last variable is the array’s length minus one.

An Arduino board connected to a computer for programming

An Arduino board connected to a computer for programming

Let’s consider this array.

int exampleArray[] = {4, 5, 6, 7, 8, 9, 10};

We can use this code to access and print the first variable (index 0).

int x = exampleArray[0];

Serial. print ln (x);

If you want to print the last variable (10), use this line.

int y = exampleArray[6];

Serial. print ln (y);

But what if you try to access a value past the array size?

With programming languages like Java, the compiler checks if the code for accessing array values is within the boundaries of the declared array and will give you errors if you want to access data past these boundaries.

However, the Arduino C++ compiler does not do any checks.

So you must be careful when accessing array elements because you can get random data stored next to the array in memory.

Code is written on an editor before compiling.

But reading this memory is not a significant issue because you’ll only retrieve invalid data. For instance, with the array declared above, this line of code will display invalid data as the serial output.

Serial.println (exampleArray[7]);

The problem comes when you write data to these random memory locations.

You can create bugs that are difficult to trace or cause the program to malfunction/crash.

Arduino Arrays and FOR Loops

Another way of accessing arrays is using FOR loops.

This loop creates a counter that you can use to print or display the individual array elements one after the other.

A 2D array with a nested loop for accessing the stored elements

Consider the following array.

int myArray[] = {3, 6, 7, 8, 10, 15};

Use the following code to display these array elements over the serial port with a FOR loop.

for (int i=0; i<6; i++){

Serial.println(myArray[i]);

}

Remember, array indexing begins from zero, meaning myArray takes the indexes 0-5 in memory space.

So we’ll use i=0; i<6 with the incremental operator to go through the five indexes during each loop to print the array value represented by i.

Wrap Up

As you can see, checking the Arduino array length requires the size of the () function, but with a twist. 

Since the function returns the size of the array in bytes, you have to divide it by the size of its data type to get the number of elements in the data structure.

And remember, character arrays will have an extra element, so you’ll have to deduct it to get the correct array length. 

That’s it for this article.

Contact us if you need help implementing this function. We’ll be happy to help. Have a good one!

Categories How