Contents

Introduction
1
Course Overview
PREVIEW2m 2s
Conclusion

The course is part of this learning path

Pointers
Difficulty
Intermediate
Duration
1h 25m
Students
41
Ratings
5/5
starstarstarstarstar
Description

In this course, we will explore the basic concepts and fundamentals of pointers in C++ and you'll learn how to use them to point to dynamically allocated memory.

 

Learning Objectives

  • Learn the fundamentals of pointers
  • Learn how to allocate memory dynamically and also how to return it
  • Explore rectangle and circle classes and create instances of them dynamically

Intended Audience

  • Beginner coders, new to C++
  • Developers looking to upskill by adding C++ to their CV
  • College students and anyone studying C++

Prerequisites

To get the most out of this course, you should have a basic understanding of the fundamentals of C++.

 

 

Transcript

In this lecture we will begin our discussion of pointer variables usually just called pointers. A pointer holds a memory address just like ants hold integers, doubles hold double values and chars hold character data types like ant and double hold their data directly. And pointers refer to the data that we're interested in indirectly. The reason they have the name pointers is because they point to the data held in memory rather than holding that data directly in order to declare a pointer, we use the asterisk placing it between the data type and the identify their note that if we say something like star my PTR that is not and integer. It is a pointer containing the memory address of where the integer lives in memory. Likewise, double star. Some double pointer is not a double. It's a variable capable of holding the address of a double in memory. Also note that we have the letters PTR either in capital or lower case, it's common to put the letters PTR as part of the pointer variable identify their so it is immediately obvious that the identified as that of a pointer and not some other type of data. Additionally, if you do use multiple variable declarations per line, you must place the asterisk next to each of the variables it does not distribute. So I know I'm flooding your brains with a lot of information right now. So hopefully an example will help solidify some concepts. Let's create a project called pointer fund create a new project. Empty project. And we will call it a pointer. Fun. Alright, so let's create a main file, add new item. Main dot CPP. There we go. So let's work with some pointer syntax so you can see it in action. Yeah. All right. There's maine So here is my lovely integer and it's 150. So no pointers yet Justin Plain old integer. one 150 is its value. Now, what I'm going to do is I'm now going to create a pointer and I'm going to assign its value to the address of my lovely integer. We'll talk a little bit more about the syntax in a moment but in star some PTR Now when I declare it as a single point or a single variable, I put the star myself next to the end but actually you could put it in between spaces on either side or you could actually put it right next to the name. So that is actually pretty common as well. But I like putting it next to this because it is a and into your pointer. That's the way I look at it. So this is intense. Star some PTR it's not an integer, it's an integer pointer equals and I can't say equals my lovely into because then it will try to say well, memory address 1 50 what does that mean? You have to get the address of my lovely hint. So that ampersand in this context just like we've used it for references before is used to get the address of my lovely end. Now let's print out some stuff. So my lovely aunt is originally I know I don't have quotes on it. We need some quotes here, don't we? All right. So my lovely ent is originally my lovely aunt and then we have pointer holds value some pointer. Then we have pointer the referenced. Okay we'll talk about what all this means in just a moment. Some pointer And I'm going to set some pointer equal to 200 notice I'm using the star in front of it. We'll talk about that. And then I'm going to put my lovely end is now all right and of course let's run it. Debug. Start without debugging. See what we get. There's a lot of explanation to go on here that we'll talk about just a second. So what I have is it will say my lovely aunt is originally 1:50. That's what we expect because that's pretty much the old stuff we've always been doing for a long time. If you print out the pointer directly it prints out this kind of weird number here. 00 76 FEA eight. So this is actually a memory address and then below it it says pointer de referenced. If I put the star in front of the pointer, you'll notice it's actually returning the thing at that point or like what does the point to point to? It says 1 50 And then you'll notice when I say star some point equals 200. And then I print out my lovely aunt, I don't get 150 again Get 200. So what's going on here formalize some of the things I've said and review it. So there's a lot to explain. First we declare the integer named my lovely aunt and store the value 1 50 in it. Then we create a pointer named some pointer and send it to the address of my lovely and using what's called the address operator. Also sometimes called the address of operator which is the ampersand the operator pulls double duty. So we've seen it before when we pass parameters by reference this time we are using it as a unitary operator to obtain the address of whatever variable you apply it to. And since pointers hold addresses, this is a match made in heaven. Some pointer now holds the address of my lovely and cool. Huh? Then we print a bunch of stuff. So first we're printing the original value of my lovely aunt and then the contents of some pointer. So what's with the strange letter and number combination that we get when we run this. So run it again, we have this strange letter number combination right here. Very very unusual. Right so This is actually a number in base 16 which is called hexi decimal without getting too much into the math behind it. Right now in computer science we often Work with base 10 which is what people work with most of the time. Almost all number systems on the planet developed were based n not all but most because 10 fingers, 10 toes, humans are the one that developed it. That kind of makes sense When you were doing commerce back in the day, everyone said okay 10 10 seems to be a pretty magic number. Um some societies developed a base 20 because maybe they could see their toes all the time wearing sandals and warm weather Mayans being an example, but most societies did develop or get from other societies a base 10 system and that's the one that's most popular throughout the world. This is called the decimal system. That's pretty much familiar to all of us. Base to which is called binary instead of decimal, it's binary. These are the zeros and ones that computers like to use. And a lot of times We also work with base 16, this is called the hexi decimal system. Like the values we see here, Any individual single digit goes from zero up to 15. But How do you represent 10 through 15 Using a single digits. So we have a little bit of a problem there In base 10 we go from 0 to 9 because the nine is less one less than the name of the base. So we know we can go from zero up to number nine. Now if we count to those numbers, zero is the first number one is actually the second it's almost like indexes with a raise But there are 10 available values For that single digit in base 10 In base two we can only fit two But it's zero and 1. What about base 16? Well base 16 goes from zero through 15, that's 16 available values. But herein lies the problem. This is now more than what we can represent in the decimal system. So we start using Letters of course. So a is equal to 10 and B is 11, C is 12 etcetera. So what are we doing with the asterisk? What are we doing with the asterisk next to the name of the pointer here and we get this 150. So the asterisk is being used as a unitary operator on variable some pointers. So it's kind of weird because we've used it before. It's actually used in many different circumstances and we'll kind of review those in just a moment but in this case we're not declaring the pointer clearly. So what are we doing with the point when you d reference a pointer? A process sometimes called indirection. You get the value that the pointer is pointing to. In other words it goes to the memory address that the pointer contains and then returns the data that lives there at that memory address. So after doing our initial printing, you notice our initial printing that we d reference the pointer once again but this time it's not just grabbing the value out and printing it like we did up here, it's setting the value to a different value. So we are setting the value that the pointer points to buy. De referencing it to the value 200. So we're not changing the memory address. We're going to that memory Address and saying, Hey This is now 200. So this should have an impact on the variable we're pointing to because they both refer essentially to the same memory address. This one actually lives at that memory address and then some pointer is actually pointing to it so it's referring to it And it does in fact affect it because when we print it out we don't get 150 anymore. We get 200. So you can change the value of this variable. My lovely end using this variable within direction. The pointer. Pretty awesome. So as a brief recap and to help you organize some of what we're learning. We've seen the asterisk being used to do a lot of different things in this entire course. In fact, in fact, if we Pool Our knowledge, we can see three situations in which the asterisk has been used between a data type and then identify rare to use in declaration of a pointer to that type of data such as in pointer my number as a unitary operator. That is an operator with a single operandi in front of a pointer variable name used as the D reference operator in a process. We call indirection or de referencing the pointer and also as a binary operator used for multiplication. So if you have two variables that can be multiplied and you can use it as that. So depending on its context, it means different things. Another point to make is that sometimes the term raw pointer is used to distinguish the pointers. We will be exploring in this section from a newer feature in C plus plus, introduced in the C plus plus 11 standard called smart pointers. These are also known as managed pointers but we won't be covering them in this section. So keep that in mind. If you come across any of this terminology, before we move on to the next lecture, I have a challenge for you. You can continue using this project and just add the challenge material to the current source file. I would like you to declare and initialize a double Named my double and set its value to 3.14 I know really original. Then declare a pointer to a double named double PTR and set it to the address of my double. Finally near the end of maine print out both the actual contents of double pointer as well as what it points to use indirection to get the value that it points to. So pause the video and come back when you're done or if you'd like to see how I do it. How'd that go for you. Were you able to complete this challenge? Let's work on it together. I'm gonna close this right here Up near the top here. I'll just keep things grouped here. So double my double equals 3.14. And then all group these. Double pointer, double PTR equals the address of my double. Right. And then of course we want to print out the double point or I guess or the double in both ways. So let's print it out down here. So I'll print out the double pointer, the actual value that will be its address or the address rather than its pointing to. And then star double pointer to use in direction. Now let's run this just to make sure we did it. Right. Good. So we get a memory address right Here and we get 3.14 at the bottom right here with the actual values. So that's pretty cool. Right. Okay. Everything seems to be right. So you might have noticed the memory addresses are changing when we run our applications. It's not always the same when we rerun it. This is to be expected because the variables have different memory addresses when they have memory allocated to them with each one. So it might seem like pointers have limited applicability. Like why do we need to point to other variables that we can declare statically in our code anywhere automatically. So that's not usually how we use pointers As we're about to see in the next lecture, we will begin our discussion of dynamic memory allocation. I'll see you there.

 

About the Author
Students
1352
Courses
20
Learning Paths
4

John has a Ph.D. in Computer Science and is a professional software engineer and consultant, as well as a computer science university professor and department chair.

Covered Topics