Arduino Length of Array

This tutorial will discuss a method to get the length of an array using the sizeof() function.

Get the Length of an Array Using the sizeof() Function in Arduino

To get the length of a given array, you can use the sizeof() function. This function returns the number of bytes present in a variable or an array. This function takes an input variable of any data type and returns the number of bytes occupied by that variable. To get the length of an array, first, get the number of bytes present in the given array using the sizeof() function, then divide it with the number of bytes present in the array data type.

void loop(){
    int myarray[5] = {19, 10, 8, 17, 9};
    int size = sizeof(myarray) / sizeof(int);
}

In the above code, size is a variable of type int to store the given array’s length, and myarray is the given array of any data type. Note that in this example, we used an array of type int; that is why we divided the given array size with the size of an int. You can change that according to the data type of the given array. Check this link to get more information about the sizeof() function.

Contribute
DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - Arduino Array

  • Arduino 2D Array