***Learn to program and prototype with Arduino now! Join our membership.*** ***We designed this circuit board for beginners!*** Kit-On-A-Shield: FOLLOW US ELSEWHERE --------------------------------------------------- Facebook: Twitter: Website: ________________________________ So let me ask, what is a pointer? Well, a pointer is a variable that holds the memory address of another variable. Hmm, okay, let's break this statement down. The first part says, a pointer is a variable. Now, hey, I kinda like this part the best, because probably like you, I'm super familiar with variables. In the Arduino programs that I write, I use variables all the time like a constant byte variables for pin numbers or integer variables for different sensor readings or float variables for some type of precise calculation. I mean, the list goes on. I mean, variables are everywhere in my code. Well, pointers are variables too. So you might actually know a lot more about pointers than you actually realize. So let's talk about a couple things. First, pointers point to a specific data type. So just like an integer variable is designed to hold integer values and not like float values, an integer pointer is designed to point to an integer variable, not a float variable. Declaring a pointer is super similar to declaring any other variable. If I wanted to declare a float variable, I need the data type and the name of the variable, like float temperature, right? So now I got a variable called temperature and it's gonna hold a float value. If I want to declare a pointer, I need the data type the pointer variable will point to, the name of the pointer variable and an asterisk right before the name. So here we have a pointer pData that will point to an integer variable. Now listen, I know what you're thinking. What the heck is that asterisk for? That asterisk is called the dereferencing operator. It's also known as the indirection operator. It's used when declaring a pointer so that the Arduino IDE knows that this is no regular variable that this is a pointer variable. And as you'll learn, pointer variables get handled differently than normal variables. And the asterisk tells the Arduino ID, “Hey, I'm not just some regular variable, I'm a pointer variable.“ So anytime you declare a pointer variable you need to use that asterisk in front of the pointer variable's name. Pointers also have the same naming rules as variables. The name of the pointer can be composed of upper and lowercase letters, digits and underscores. You can't start the name of a pointer variable with a digit and you can't use any reserved keywords for the pointer name like int or true. One final thing to point out about this naming. Do you notice I started the name of the example pointer with a lowercase P? By convention, the name of a pointer starts with either a lowercase P or ptr, followed by the name of the variable it points to. This is meant to help inform the people reading the code that, hey, this must be a pointer variable because it starts with a lowercase P. So if we have an integer variable named data then by convention, a pointer to this integer variable would be named pData or ptrData. Again, a convention is just something everybody kind of agrees upon. If you do it differently in your code your code is still gonna run okay, but other developers who read your code may get a little confused. All right, so let's review what we know about pointers so far. Pointers are variables. Every pointer must point to a specific data type. When declaring a pointer, you use an asterisk before the name. Pointer variables have the same naming restrictions as normal variables. By convention, Pointer names start with a P followed by the name of the variable they point to. All right, so now you know that a pointer is a variable that holds the memory address of another variable. This is a really important idea to try to get in your head but you're probably really fuzzy about, well what is a memory address and what is the use of a pointer? I'm not sure I get any of this. That is perfectly alright. This is kind of gonna be a a long discussion that we have as we develop the understanding of what pointers are and how to use them. In the next lesson, we're gonna start talking a little bit more about memory addresses. ***About Us:*** This Arduino lesson was created by Programming Electronics Academy. We are an online education company who seeks to help people learn about electronics and programming through the ubiquitous Arduino development board. ***We have no affiliation whatsoever with Arduino LLC, other than we think they are cool.***
Hide player controls
Hide resume playing