Book Image

Learning C for Arduino

By : Syed Omar Faruk Towaha
Book Image

Learning C for Arduino

By: Syed Omar Faruk Towaha

Overview of this book

This book will start with the fundamentals of C programming and programming topics, such data types, functions, decision making, program loops, pointers, and structures, with the help of an Arduino board. Then you will get acquainted with Arduino interactions with sensors, LEDs, and autonomous systems and setting up the Arduino environment. Moving on you will also learn how to work on the digital and analog I/O, establish serial communications with autonomous systems, and integrate with electronic devices. By the end of the book, you will be able to make basic projects such as LED cube and smart weather system that leverages C.
Table of Contents (17 chapters)
Learning C for Arduino
Credits
About the Author
About the Reviewer
www.PacktPub.com
Customer Feedback
Preface

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...