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:
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:
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:
// 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:
// 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:
// 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:
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
- The Basics of C++ on an Arduino, Part 1: Variables
- The Basics of C++ on an Arduino, Part 2: Functions and Methods
- The Basics of C++ on an Arduino, Part 4 Control Statements and Loops
- The Basics of C++ on an Arduino, Part 5: Software Libraries and Custom Classes
Summary
You can use arrays to group similar related values. Note that you can only store values of the same type in a single array. Arrays have a fixed length, and you can access their items with an index. These indices start at zero. You can also use a pointer to access the elements of an array. Use the ampersand operator to obtain the address of a variable. Utilize the asterisk to dereference a pointer and read the value of the variable that it points at.
Have questions or comments? Continue the conversation on TechForum, DigiKey's online community and technical resource.
Visit TechForum