Arduino Pointers and Memory Addresses #Arduino #C #C++

Programming Electronics Academy
Programming Electronics Academy
8.9 هزار بار بازدید - 2 سال پیش - 🤩 FREE Arduino Crash Course
🤩 FREE Arduino Crash Course 👇👇
https://bit.ly/get_Arduino_skills

**Learn to program and prototype with Arduino now! Join our membership.**
http://bit.ly/3hvsDqf

**We designed this circuit board for beginners!**
Kit-On-A-Shield: https://amzn.to/3lfWClU

FOLLOW US ELSEWHERE
---------------------------------------------------
Facebook: Facebook: ProgrammingElectronicsAcademy
Twitter: Twitter: ProgElecAcademy
Website: https://www.programmingelectronics.com/
________________________________
Hello, I hope you're doing fantastic. All right, so you might be wondering what are these memory addresses? What are they actually doing? What purpose do they serve? Well, let me tell you, these addresses are key for how the processor will know where to look for data when it's running your program, they literally direct the processor where to put data in memory, and where to find the data from memory. So let's think about a physical address for a moment. Say I live at 42 Honolulu Blvd., that's like my address. if you wanna find me. You open your phones mapping application and say take me to 42 Honolulu Boulevard. Then you hop in your private Learjet and fly over, you grab an Uber to 42 Honolulu Boulevard and what are you gonna find there? Well, you're gonna find me, that's my address. Now, I may be out practicing my nunchuck skills or whatever, but nonetheless my address will point to where I am. So what do these memory addresses have to do with pointers? Well, kind of everything. So let's go back to our pointer definition. A pointer is a variable that holds the memory address of another variable. So a pointer holds the address of another variable. If I had an integer pointer named pScore, which pointed to the score variable, then pScore would be holding the value 104. Likewise, if I have a float pointer named pResult, which pointed to result, then pResult would be holding the value 106, so these pointers hold addresses If I had an integer pointer bucket, I would put integer addresses in that bucket. If I had a float pointer bucket, I could put float addresses in that bucket. So how do we get these variable addresses, and how do we get a pointer to point at the address? So let's start with the first question. It turns out, to get a variables address is really simple, you just use the & operator in front of a variable name. This is also called the reference operator. It's literally that easy, you just put the little & sign in front of a variable name, and now it's gonna give you the address of that variable in memory. Now listen, you probably don't believe me, hey, fair enough, I don't really believe me either, so why don't we put this to a test? What if we make some variables and then print their addresses to the serial monitor window using the ampersand operator in front of the variable name? So here we are in the Arduino IDE, what I'm gonna do is create some variables, then I'm gonna print the memory addresses of those variables using the ampersand operator, and this is gonna prove that I'm not lying to you. So let's do this. All right, so I've created four variables, a byte, a character, an integer, and a float. We've set them equal to different values. So I did this first part, created some variables. Now what I'm gonna do is print the memory addresses of these variables, using the ampersand operator. So let me do that. So here I'm printing &ledPin, so this is at ampersand operator, and this is saying, give me the address of ledPin. Now here's the deal, and this might sound a little weird but just wanna try to explain this as best I can. Serial  print is not designed to be able to display an address, a variable address, so we have to cast the address as something else, something that Serial  print can print, like an integer or a long, and a common way to do this is to cast it as an unsigned long. So I'm gonna do that right now just to show you. So what this section of code right here is doing is it's taking the address of ledPin, and it's casting it as an unsigned long, and that's simply so that we can print the address out on the serial monitor window. All right, so I'm gonna do the same thing for these other variables and then we'll see the results on the serial monitor window to determine whether or not I am a cold, hard liar. All right, so let's take a look at the output here. Our byte ledPin is being stored at memory address 2299, and we said that a byte takes up one byte of memory, so where would the next variable get stored after 2299? Be it 2298?

**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.**
2 سال پیش در تاریخ 1401/08/20 منتشر شده است.
8,934 بـار بازدید شده
... بیشتر