Maker.io main logo

The Basics of C++ on an Arduino, Part 3: Pointers and Arrays

2020-11-04 | By Maker.io Staff

This Basics of C++ on an Arduino series is covering many different elements necessary for all sorts of projects and ideas on an Arduino. In this entry, we cover Pointers and Arrays in C++.

So far, you’ve seen how you can store a single value in a variable. However, what if you want to save multiple values as a recognizable group? Arrays are one method you can use to accomplish that. While they aren’t able to store multiple values in a single variable, they allow you to save several related values in the Arduino’s memory as a coherent group.

The Basics of Arrays

Arrays are exactly what their name suggests: an arrangement of values with the same type. The elements of an array usually get placed in the memory one after the other.

Let’s suppose you want to store 128 values that you received from a temperature sensor. With what you’ve learned so far, you could only create 128 individual variables like so:

Copy Code
float measurement_1 = 19.8f;
float measurement_2 = 22.21f;
float measurement_3 = 23.6f;
float measurement_4 = 17.42f;
// …
float measurement_128 = 31.63f;

Besides being a pain to write and maintain, there’d also be no easy way of handling all these variables. If you wanted to print their values, for example, you’d have to make 128 individual calls to the print method.

As mentioned above, you can use arrays to create a coherent field of similar values. For that, you have to define the type and the name of the array, just like you’d do with any other variable. However, you then add square brackets to indicate that you want to create an array. Inside of the brackets, you define the size of the array:

Copy Code
float measurements[128];

This will create an empty array of 128 float variables. You can, however, also initialize an array by defining a list of values that you’d like to store:

Copy Code
// This will store the five characters
char text[] = {'H', 'E', 'L', 'L', 'O'};

// You can also add the length of the array and define a list of values like so:
String names[6] = {"Ben", "Sarah", "Julia", "Peter", "Michael", "XR30-Z"};

Note that arrays always have a fixed length that you can’t change later on.

Accessing Array Elements

You can access the elements stored in an array by writing its name followed by an index that’s enclosed by square brackets:

Copy Code
// Gets the first element of the array
float value = measurements[0];

// Gets the last element of the array
float value = measurements[127];

// Read some other value
float value = measurements[51];

Array indices always start with zero. If you request an index that’s too small or too large, you’ll produce a runtime error or non-deterministic behavior.

A Short Introduction to Pointers

Another way of accessing array elements is to use a pointer. You can think of a pointer as a thing that, well, points at a specific location in the Arduino’s memory. Defining a new pointer works similar to creating a variable. You start with the type and the name. However, pointers get marked with an asterisk symbol that comes after the type:

Copy Code
// The array from before
char text[] = {'H', 'E', 'L', 'L', 'O'};

// A pointer pointing at the address of the first element of the text-array
char* beginning = &text[0];

// A pointer that points at the beginning of text
char* alt_beginning = text;

As you can see, you can put an ampersand in front of a variable’s name to get its address, which can then, in turn, get stored in a pointer. One huge advantage of pointers is that you can use simple arithmetics to change the address that they reference:

Copy Code
char H = *(beginning + 0);
char E = *(beginning + 1);
char L1 = *(beginning + 2);
char L2 = *(beginning + 3);
char O = *(beginning + 4);

You must note two things: Firstly, there’s another asterisk in front of the address that you want to read. This is called dereferencing, and it simply means that you aren’t interested in the address, but rather the value that’s stored at that address. Furthermore, the addition happens inside of parenthesis. This is important because otherwise, you’d first dereference the pointer value and then add a number to it, which would lead to a different value.

Recommended Reading

制造商零件编号 A000066
ARDUINO UNO R3 ATMEGA328P BOARD
Arduino
¥224.66
Details
制造商零件编号 A000005
ARDUINO NANO ATMEGA328 EVAL BRD
Arduino
¥202.68
Details
制造商零件编号 A000067
ARDUINO MEGA2560 ATMEGA2560
Arduino
¥393.97
Details
制造商零件编号 ABX00033
ARDUINO NANO EVERY WITH HEADERS
Arduino
¥119.66
Details
制造商零件编号 ABX00012
ARDUINO MKR ZERO W/ HDR ATSAMD21
Arduino
¥246.64
Details
制造商零件编号 A000073
ARDUINO UNO SMD R3 ATMEGA328
Arduino
¥214.08
Details
制造商零件编号 A000057
ARDUINO LEONARDO W/ HDRS ATMEGA3
Arduino
¥202.68
Details
Add all DigiKey Parts to Cart
TechForum

Have questions or comments? Continue the conversation on TechForum, DigiKey's online community and technical resource.

Visit TechForum