C++ Pointers - Finally Understand Pointers

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone it's caleb welcome in this video we're going to be talking about pointers in c plus plus and this is pretty much going to apply to c as well so if you're brand new to this subject it can be a little bit complicated so make sure you pay attention and i'll try to give you some good tips along the way or should i say pointers anyways check out our sponsor c plus plus builder is the ide of choice for rapidly building c plus applications utilize drag and drop visual components that are responsive and allow for cross-platform deployments when building data-driven applications you can bind data sources to visual components to make working with data easy go ahead and get started with a free trial of the architect edition which will give you all of the features of c plus builder so whether you're a beginner just getting started or want to build enterprise level applications c plus builder is the tool for you i'll leave a link in the description so first thing before we write anything and try to understand the concept you should know that this is going to be a purely conceptual video and then we're going to follow up with the hands-on content also i really recommend you first study references so we did some videos on references and the rule is something like use references when you can and then what's what's the phrase use pointers if you have to or something like that basically when you're in doubt go for references and if for some reason you can't do what you're trying to do with references but you can do it with a pointer then you should consider pointers don't just jump in and be like oh yeah i'm in c plus i got to use pointers you know it's going to make me cool and get jobs no it doesn't really work that way in fact pointers are usually avoided unless they are necessary so figure out references and then we'll talk about some of the differences between references and and pointers in this video and some of the upcoming content all right so we got that on the chalkboard references first next up let's just talk about the absolute basics how to create a pointer what it actually even is why you might want to use one and all of those things so very first thing let's talk about creating a variable inside of c plus you say something like let's go with intex has the value 5. hey we just created a variable pretty simple you should probably know how to do this so right now we have this variable x and it is of type int so what exactly does that mean well it means we have an area in memory that contains the value 5 and we can refer to it with the identifier x but this isn't the only thing in memory obviously there's all kinds of variables in memory and all of your programs run in memory so you need a way to basically say where this is located in memory when you're programming you don't have to worry about this when you're just working with x all that's going to happen for you however if you want a little bit more control or visibility that's where pointers come in so we can actually point to this memory address using a pointer all right so before we talk about how to create the pointer let's talk about a really useful operator and that is the address of operator it's kind of a weird name so address of operator dude i don't i can never remember if it's or er so this is a unary operator what that means is it works on one operand so you just attach it to one thing and it can be the variable you're working with so basically when you read this you you would say address of x what does that mean well it's actually going to get the memory address and it's going to probably be like 12 hexadecimal digits i don't know why it people say that because like digits implies base 10 but hexadecimal is like base 16. so hexadecimal numbers so you're gonna probably have 12 of those so it might look something like this i'm just making this up so these numbers aren't significant all right so one two three four five six seven eight nine ten eleven twelve and basically this is hexadecimal so this would coordinate to 48 bits in memory but you don't really have to worry about that basically the point is you can use this address of operator to get the location of this data right here and you don't just want to print that out i mean you can you could just you know say c out like so you could output that to the console but you you might want to save that and use it later in your code and that my friends is where the pointer comes in so we can assign this to a variable and use it later so what in the world is that going to look like well it's going to look like this we're going to say int and then to say that this is a pointer you use the asterisk and then give it a name so for example y and then you assign it the address of x in this case so this is the syntax to create a pointer of type int so it's not an integer it's an integer pointer these are two separate types in c plus plus so int pointer is not the same thing as int sorry i hesitated there because i'm doing like the math notation so this is not the same thing as an int and if you try to use them interchangeably you're going to get type errors things aren't going to compile or your code's just going to not work the way you'd expect so just be aware these are two separate types when you need a pointer you're going to use y when you need an integer you will want to use x or you can actually get the data that y points to and that's what we're going to talk about now and again this is kind of one of those things where it's like a lot of information so you might need to watch this a couple times or do a little extra research i'm trying to give it all in a fluid way however it's kind of like this thing where it's like oh you also need to know about this and this and this and then everything kind of pieces together so let's just talk about what this actually looks like right now we got this place in memory and it contains the value 5 and we refer to it as x now we introduced this new variable and it is called y and this is a pointer so instead of containing the data here it points to this location in memory this is very similar to how references work so that's why there's a lot of confusion on when to use which so if you want to get the integer data you can do it now two ways you can use x that's going to give you the value 5 or you can actually tell the computer to get the data wherever y points and the way you do that is with the asterisk so you would say asterisk y it's a terrible asterisk i'm so sorry guys something like that so asterisk y that is also going to give you the value 5. so if you were to compare these x being equal to asterisk of y this should evaluate to true now i've been saying asterisk y but that's not the official technical term of this this is called the dereferencing operator so we are dereferencing y to get the value that it points at now we're working with an integer so just to repeat that y is a pointer dereferencing y gives us an integer integer pointer integer integer pointer integer say with me guys now i'm not going to make you do that all right so this is where like i got the most confused when i first learned about planers and c programming c programming disgusting so you define the pointer using the asterisk okay you got that so far you can de-reference the pointer using the asterisk but these do not have the exact same meaning this is saying hey we're creating a pointer this is saying hey get the value wherever that pointer points to but you don't have to use that asterisk whenever you're working with that pointer you just use y by itself so if i asked you hey could you write out the pointer you just created you would say why and it's confusing because when you define it you use the asterisk but then you don't ever use the asterisk again until you want to de-reference it so asterisk to create the pointer you use y by itself whenever you're talking about the pointer variable and then asterisk y to get the data where the pointer points to it's crazy all right why why would you want to do any of this stuff well again use references first because you can do a lot of this stuff with references however let's just for a second assume you had to use pointers maybe you're using c programming or you're in school and they're making you use pointers for some reason well in that situation you can basically create two ways of working with the same data to draw what i just had this contained the value 5 and you can refer to this data with x or by dereferencing the y pointer so now you have two ways to refer to the same location in memory so that's the first reason you might want to do this if you need multiple variables to talk about the same area of memory you can use pointers this is commonly used for functions and passing data so if i create some function and you know it has a parameter in there we'll just call it data well if you pass in x by default that x gets copied the data of x gets copied into this data parameter well if you're using pointers you can actually have them refer to the same area of memory so if instead you passed in y and the function took a pointer well then now the address is going to be copied into data and y is going to point to the same area of memory and data is going to point to the same area of memory so what's the point now inside of this function this function can change that data so you know we could assign it the value 10 and it's going to change that specific area of memory so that change is going to be seen everywhere not just within that function so basically we can use pointers to allow us to change data that's defined outside of the function we can change the argument variables that are passed into the parameters and we'll see that more hands-on in the upcoming videos don't worry about all the syntax right now just understand the concept that it allows our functions to change data that's passed in and it's going to persist beyond that function call you might see this for a swap function so you could create a swap function to actually swap two variables this is actually kind of hard to do in some other programming languages that don't have pointers or references but in c plus plus it's really easy to do you can do them with both pointers and references because you can think of it like so if we have some function swap and it takes an input a and input b and it swaps the data well in order for that change to be seen over here it kind of looks like a smiley face or like the amazon logo in order for those changes to be seen after the function call that data that's passed in needs to be able to be changed and again that can be done with pointers and references so any times you're working with dynamic memory where you have to allocate memory yourself you're going to be using pointers so in c plus when you see the new keyword that is dynamic memory allocation and it's going to return a pointer and you use that pointer to refer to that area of memory don't do this unless you have to and if you do have to do dynamic memory there's smart pointers which will kind of abstract away the memory management aspect of this so you can study into smart pointers and the different types inside of other programming languages you know c sharp java you will see the new keyword quite often in c plus you don't want to use the new keyword unless you absolutely have to in fact when you create an object in c plus by default you don't want to use the new keyword so if you're creating a user object it's just going to look like this user u you're not going to do something like this this is what you would see in java as an example but in c plus this new keyword here is going to put this data on the heap memory and you're going to have to later free this memory and you're gonna have to worry about that so don't do that unless you absolutely have to for some reason such as if you need this to extend beyond the scope it's defined in but we're like really getting into the weeds here so let's not worry about that right now so i think that's going to conclude my introduction to pointers check out the next video because we're going to get hands-on with it and if you're looking for a little bit of extra study i suggest you look up differences between references and pointers so we talked about references we talked about pointers now you just need to figure out when you want to use which and again just to repeat myself for like the fifth time use references unless you absolutely have to use a pointer for something but most of the time that's not going to be necessary for example i did a 10 hour c plus series here on youtube and everyone was so upset because i didn't get into pointers it's because i had absolutely no need for the pointers in the introductory course yeah maybe you should have taught them just for practice but everyone jumps into c plus plus wanting to use pointers but you don't actually need to use pointers for all that much so stick to references unless you have to use pointers thank you guys please be sure to subscribe smash that sub button slap it all right i'm uh i'm tired of talking about pointers so bye you
Info
Channel: Caleb Curry
Views: 50,895
Rating: 4.9304686 out of 5
Keywords: C++ Pointers - Finally Understand Pointers, c++, pointers, unsertand, code, coding, how to, programming, caleb, curry, computer science, smart pointers, address of
Id: rtgwvkaYt1A
Channel Id: undefined
Length: 15min 55sec (955 seconds)
Published: Fri Oct 16 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.