Source: https://mokxi.com/learn/arduino-array
Updated: 2026-10-25

Learn

# Arduino arrays: a list of things under one name

Written by the Mokxi team, updated October 25, 2026

Start building free

Or see every lesson.

- 455 parts on the bench
- 43 boards running now
- 1.00x real time, on every board

Click to open it in the editor

A for loop walks an array of eight bytes and shows each one on the lamps.

An array is a list of values of the same type under one name. int pins[] = {13, 12, 11}; makes a list of three numbers, pins[0] is the first one, and a for loop can walk the whole list. Arrays are how a sketch keeps a row of LED pins, a melody, a set of light patterns or the last ten sensor readings.

The circuit above keeps eight light patterns in an array and shows them on eight lamps through a shift register. Press Run. The serial monitor prints "patterns in the list: 8", then "step 0 = 1", "step 1 = 11" and on to "step 7 = 11111111" as the bar fills one lamp at a time. Every step is one entry from the list.

Below is how to make an array and count it, why the first entry is number 0, and the two mistakes that cost the most time: reading or writing past the end, and asking a function how big an array is.

Want the step-by-step version? The lesson "A loop over a list" walks through this with checkpoints.
Open the lesson

## Make one, and count from zero

Write the type, the name, square brackets and the values in curly braces. Leave the brackets empty and the compiler counts the values for you. The entries are numbered from 0, so an array of eight has PATTERNS[0] to PATTERNS[7], and there is no PATTERNS[8].

You can also give the size and fill it in later: int readings[10]; makes ten ints. A global array like that starts as all zeros. One made inside a function starts with whatever was in memory, so fill it before you read it.

firmware/uno/examples/cpparray (the list and the loop running above)

## How many: sizeof

sizeof gives the size of something in bytes, not the number of entries. So the count is the size of the whole array divided by the size of one entry. For five ints on an Uno, sizeof(scores) printed 10, because an int there is 2 bytes. The same sketch on an ESP32-C3 printed 20, because an int there is 4. Divided by the size of one entry, both said 5.

Work the count out this way rather than typing 8 by hand. Add a ninth pattern to the list above and press Run: the first line becomes "patterns in the list: 9", the loop runs nine steps, and you did not touch anything but the list.

The size in bytes, and the count

## An array of pins

The most useful array on an Arduino is a list of pins. Put the pin numbers in an array and one loop sets them all up, and another lights them in turn. The sketch below runs on the three lamps of the code bench (pins 13, 12 and 11) and printed "pin 13 on", "pin 12 on" and "pin 11 on" in order. Add a pin to the list, wire one more LED, and it joins in.

Arrays of text work the same way. const char *names[] = {"red", "yellow", "green"}; is a list of three words, and names[1] is "yellow". The random numbers page uses one to make a magic eight ball.

An array can also hold rows. const byte GRID[2][3] = {{1, 2, 3}, {4, 5, 6}}; is two rows of three, GRID[1][0] printed 4, and sizeof(GRID) printed 6, two rows of three bytes. Two for loops, one inside the other, walk every entry, which is how a sketch keeps the dots of an LED matrix or the keys of a keypad.

A list of pins

## Past the end: no error, wrong answers

Nothing checks an index while the sketch runs. Ask for entry 5 of a five-entry array and the board does not stop or complain; it reads whatever two bytes come next in memory. Our a[5] printed 280, a number that was never in the list. The build tab did warn about that one, because the 5 was written right there in the code: array index 5 is past the end of the array (that has type 'int[5]').

Writing past the end is worse, and usually gets no warning at all, because the index is a loop counter. We filled a five-entry array with a loop that said i <= 5, and a variable called other, declared on the next line, changed from 99 to 0. Nothing was printed and nothing crashed. A sketch that does this goes wrong somewhere else entirely, which is why the bug is so hard to find. Use < against the count, every time.

One past the end

## Passing an array to a function

An array handed to a function arrives as the address of its first entry, not as a copy of the list. That has two effects. The function can change the caller’s array, which is often what you want. And inside the function, sizeof no longer knows how long it is: the same scores that measured 10 bytes in setup() measured 2 inside a function, the size of an address on an Uno. The build tab warned: sizeof on array function parameter will return size of 'int *' instead of 'int[]'.

So pass the count along with the array, as average(scores, 5), or work it out with sizeof where the array was made and pass that. Every Arduino library that takes a buffer does it this way.

## Questions

Do Arduino arrays start at 0 or 1?

At 0. The first entry of an array is name[0] and the last of an array of eight is name[7]. A loop over it is for (int i = 0; i < 8; i++).

How do I find the length of an array in Arduino?

Divide its size in bytes by the size of one entry: sizeof(list) / sizeof(list[0]). This only works where the array was declared; inside a function that was handed the array, sizeof gives the size of an address instead.

Can I change the size of an array?

No. Its size is fixed when the sketch is built. Make it as big as you will need and keep a separate count of how many entries are in use.

What happens if I go past the end of an array?

Nothing stops it. Reading past the end gives whatever is next in memory, and writing past the end changes another variable. In our test, writing one entry too many turned a neighboring variable from 99 into 0 with no error.

Can an array hold text?

Yes. const char *names[] = {"red", "yellow", "green"}; is an array of three strings, and names[0] is "red". A single piece of text is itself an array of char, ending in a zero byte.

Related

## Keep going

Arduino for Loop: Count, Repeat, Chase LEDs Arduino Functions: Write It Once, Call It Anywhere Arduino Random Numbers: random() and randomSeed() Shifting a Byte In, One Edge at a Time Arduino "Low Memory Available" and "Sketch Too Big" Arduino 2D Array: Rows, Columns and Strings Arduino Struct: Group Values, With Examples The 74HC595 shift register The free Arduino simulator

Sources

## Where the facts on this page come from

- Arduino reference: array
- Arduino reference: sizeof()

## Build this for real

Open the editor, change a value and watch the number move with it. Nothing to install, and no account needed.

Start building free Open the editor
