Reader small image

You're reading from  Learning C for Arduino

Product typeBook
Published inMar 2017
Reading LevelBeginner
PublisherPackt
ISBN-139781787120099
Edition1st Edition
Languages
Right arrow
Author (1)
Syed Omar Faruk Towaha
Syed Omar Faruk Towaha
author image
Syed Omar Faruk Towaha

Syed Omar Faruk Towaha is a data scientist, and currently working at Meta. He has two bachelor's degrees, one in Physics, another in Computer Science and a Master's in Data Science. He is based in London, England, and has interests in data science and IoTs. While Syed is not busy with his jobs, he uses his time to write something about new technologies. His recent publications include Building Smart Drones With ESP8266 and Arduino, Introduction to Rust Programming, Learning C for Arduino, and JavaScript Projects for Kids.
Read more about Syed Omar Faruk Towaha

Right arrow

Chapter 7. Using Pointers and Structure

In this chapter, we will learn about pointers and structure of C programming using Arduino. We had a brief introduction to the pointer in the preceding chapter, while learning about the characteristics of object-oriented programming. This chapter will be a detailed discussion of pointers, reference points, and a new thing: structure. We will also discuss pointer types, pointers and arrays, how can we use pointers, use of structure, and so on. So, let's start our journey.

Pointers


Well, from the preceding chapter, you might remember the definition of pointers. Let's recap so that we can look at a better definition and learn about the usages of pointers.

You already know that a variable holds data. Say, an integer holds an integer number, a string holds a collection of characters, or a float holds a floating-point number. Each of the data types holds different types of data. Pointers are kind of similar to the variables, but they hold the memory location. Let's make it clear.

The variables hold information. The information is saved somewhere in our computer or Arduino. The information can be anywhere in our computer or Arduino. By declaring a pointer, we can find exactly where the variable is saved.

To make our definition shorter, we can say a pointer is just a variable, as in int, char, float, double, and so on.

Say, a box is like a variable. In a box, we can put things. So, a box can have some space. In programming, we may call it memory. Every variable has...

Pointers and arrays


Pointers not only store the addresses of the variables; they also hold the address of a cell of an array. Look at the following code:

void setup() { 
  int myVar[5] = {1, 3, 5, 6, 8}; 
  int *myPointer; 
  myPointer = &myVar[0]; 
// &myVar[0] is the address of the 1st element of myVar[5] 
} 

Say we need to print the third element of our array. In the preceding code, myPointer holds the value of the first element of our array. So, to access the third element, we need to increment our pointer by 2, as follows:

myPointer = myPointer+2; 

Now, if we print myPointer value, we will get the third element of our array. Let's look at what our program and output will be for that case:

void setup() { 
  int *myPointer; 
  int myVar[5] = {1, 3, 5, 6, 8}; 
  myPointer = &myVar[0]; // Holds the address of the 1st element.  
  myPointer = myPointer + 2;  
// Incremented by 2 to get 3rd value of our array.  
  Serial...

Usages of pointers


There are a number of usages for pointers. Some of them are as follows:

  • We can access the Arduino memory directly by using pointers

  • Pointer reduce memory wastage

  • Pointers reduce the complexity of a program

  • Pointers return more than one value to a function

  • Pointers create an alternative way to access the array elements

  • By using pointers, we can extract the address of objects

  • Pointers help to build a complex data structure

  • Pointers allow us to perform dynamic memory allocation

  • Pointers allow us to resize the dynamic memory allocation

  • Pointers reduce the execution time of a program

  • Array handling is much easier when we work with pointers

  • Since the pointers return a number, if our program deals with strings and characters, it becomes easy for us to manipulate the program

We will now discuss another interesting thing, called structure.

Learning about structure


We learned about arrays previously. An array holds elements of the same type of data. Say, an integer type of array holds only integers, or a character type of array holds only characters. But a structure is something that can hold different types of data together. To define a structure in C, we can say that a structure is a collection of variables of different data types.

Let's say we want to store information about a person, such as their name, PhoneNum, sex, and salary. All this information is not of the same type. name is a string, PhoneNum is an integer, sex can be defined by a character, and salary is a double number. So, defining one array to store them is not possible. We need to know something else that can hold multiple types of variables. To make the problem even complex, say we want to store information about multiple people.

Can you visualize how messy and long the code we will have to write will be if we want to work with an ordinary array? Well, we will...

Structure and function


Structure and function are two different things in C programming. We can pass the values of a structure to a function to make our program shorter and more efficient. There are two ways to pass a structure to a function: pass by value and pass by reference. First we will look at pass by value.

Pass by value

To pass a structure to a function, first we need to define a structure. Say we will define a structure Box, where we will have three double variables: length, height, and width:

struct Box { //declared Box structure 
  float height; //height of the box 
  float width; //width of the box 
  float length; //length of the box 
}; 

Now, we will build a function that calculates the volume of a box and prints it on the Serial Monitor:

void volume(struct Box box) { //Passed Structure 
  double volume = box.height * box.width * box.length;  //calculation 
  Serial.print("The volume of the box is "); 
  Serial.println(volume); //Printed...

Nested structure


We can declare a nested structure as the nested loop. This means we can define a structure inside another structure. The basic syntax of a nested structure is as follows:

struct firstStructure { // our first structure  
  //variables 
}; 
struct secondStructure { // Our second structure 
  struct firstStruct objName; //Created object of fist  structure(Nesting) 
  //More variables 
}; 

Let's look at an example. Let's say we need two types of information about a student, basic information and personal information. The basic information includes the name and roll number. The personal information includes age, phoneNum and sex. The structures of the two types of information will be as follows:

struct basicInfo { // our first structure 
  String name; 
  int roll; 
}; 
struct personalInfo { //our second structure. 
  int age; 
  int phoneNum; 
  char sex; 
}; 

We will nest the first structure to the...

Exercise


  • Take an input from the Serial Monitor, and display the value on the Serial Monitor using a pointer.

  • Using structure, write a program to print all the values of the structure by using a pointer and array.

  • Write a program with three structures (basicInfo, personalInfo and extendedInfo), with a number of variables. Print them from the setup() class as shown on the nested structure, but this time, use a pointer.

  • Store five values to an array by using a pointer and print them using a loop.

Summary


We have learned two confusing and great aspects of C programming in this chapter. The pointer is very useful for memory saving, since we are working with a small-memory device. A structure helps to organize and shorten our code. If we want to learn coding efficiently, we must not skip this chapter, because this chapter will help us to prepare for Chapter 8, Working with Arduino Libraries.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Learning C for Arduino
Published in: Mar 2017Publisher: PacktISBN-13: 9781787120099
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Syed Omar Faruk Towaha

Syed Omar Faruk Towaha is a data scientist, and currently working at Meta. He has two bachelor's degrees, one in Physics, another in Computer Science and a Master's in Data Science. He is based in London, England, and has interests in data science and IoTs. While Syed is not busy with his jobs, he uses his time to write something about new technologies. His recent publications include Building Smart Drones With ESP8266 and Arduino, Introduction to Rust Programming, Learning C for Arduino, and JavaScript Projects for Kids.
Read more about Syed Omar Faruk Towaha