C++ Pointers to Pointers - Finally Understand Double Pointers

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's going on everybody my name is Caleb and by the end of this video you should have a good understanding of pointers to pointers this is often called double pointers but I don't really like that name because it sounds like a pointer to a double so in this video we're just going to call them pointers to pointers it's a little extra wordy but it's a little bit more correct so what we're going to do is we're going to just go through some basic examples of where you might run into these and why you might want to use them now this is for C plus plus is what I'm going to be programming in but it should work very similarly in C and in fact you'll probably run into pointers and double did I just say double pointers in fact you're probably working with pointers and C a lot more because they don't have references so you can follow along with this video in C there just might be some minor differences when we get to the end of this video so there's two main things I can think of when it comes to pointers to pointers when you might find them and when you might want to use them the first is actually when you're working with arrays of strings this is commonly seen in the command line Arguments for the main function now I did want to mention in C plus plus I would typically use the string class over just a character array however there are scenarios where you might need to use the character array for old school C Style file strings so that's what we're going to be using in this video and that's where you're most likely going to encounter a pointer to a pointer so keep that in mind as you go through this content that you might prefer to use the string class in general but this is good to know as well which before we get started I did want to mention we have an upcoming C and C plus plus Master course so if you are interested in this it's going to be releasing soon check out the link down below to get added to the newsletter that's where I'll notify of this release and it'll be a major discount to buy the CNC plus bundle when the C bundle is released coming very soon so that's going to cover things like what we're covering in this video but in a lot more depth with a lot more examples so I think it's going to be a really great resource now let's get to the video so here we are in an MTC plus plus program if you are in C it's going to be pretty much the same except it'll just be standardio.h for the include and in either C or C plus plus you will occasionally see some parameters defined here and it's usually going to look like this in ARG C and then Char asterisk asterisk ARG V so this is one way you will see it another way you might see this is just a single asterisk and square brackets here but these are effectively the same exact thing and I'm going to explain why here soon so let's go ahead and put this back to char asterisk asterisk so this is a pointer to a Char pointer and what these names mean you can think ARG count the account of arguments and then the arc Vector which is the actual data think of a vector as like an array if you're familiar with C plus plus that's pretty common however in this scenario it's not actually a vector data type so if we wanted to work with this data what you would do is you would say for INT I is and I'll usually start with one because there's going to be an implicit argument passed in that isn't very useful to us so we can skip that I less than ARG C the count of arguments passed in and then I plus plus each iteration we will write to the console with C out ARG V index I so what this is going to do is go through any command line arguments we pass in and print them to the console so let's run this and at first it's not going to do anything but if you go back to the command you use to execute your code yours is probably not quite as complicated as this it probably is just something like dot slash pointer pointer that's the name of our file or a DOT out if that's what you have your setup as you can pass in extra data hello my name is Caleb and this can be read by the program and you can see it goes through each one of those printing it to the console so we're going to fully understand what this is and why we are using that here but to first do that I want to go with a basic example so let's go ahead and comment this out and what we're going to do is we're going to make a character array to store a string so it's going to look like this Char data square brackets hello my name is Caleb and this is a string and we can get the size of the string standard C out size of data and when we do this we should get the value 23 which is the number of characters team 20 21 22 and the null character to indicate the end of the string if we went ahead and passed this to some function let's just go ahead and call it print data we could Define the parameter here to be a Char array so we could just say Char data with square brackets just like we did down here but the data passed to this function if we said print data and passed in our data variable this is going to Decay to a pointer and it basically just loses the rest of its type information such as its size so if we then took this line down here and did it inside of print data and just to clear up any confusion we'll comment this output here so we only see one when we run this now we get the value 8. so it no longer understands the size of the data the length of the string and instead it's just giving the size of a character pointer and in fact that's what this warning is saying size of on array function parameter will return size of char pointer instead of char array so just for clarity you can actually redefine this parameter as a Char pointer and that is a pretty common convention so an alternative way of thinking of a character array is as a character pointer and it's still going to have that null Terminator at the end so we'll still know when the end of the string is reached if we want to work with multiple string literals like this what we could do is we could surround them in curly braces like so and then just do comma separated values hello your name is subscriber to do this because each one of these can be seen as a character pointer we can just see this as an array of character pointers and this will be valid code but if we then wanted to take R now array of character pointers and pass it to a function that function is going to need to be defined as a character pointer array like so or similarly as we know arrays Decay to pointers you can replace that square brackets with double asterisks here and it's going to work the same way when we do this we're not going to have the size of the array or how many strings are included in that array so we'll typically have another argument here such as size this will be passed in so we'll say size is 2 for two strings now we could do in this function is actually Loop through this data so it might look like this for INT i 0 I less than size I plus plus standard C out data and you can still use square brackets to basically jump to the next pointer in that array so data of I and each pointer in that array is going to point to a string so this should work fine standard and line so we run this we do have a warning this is just complaining about the way we defined this with the string literals basically it's going to want us to Define these as const and additionally we'll Define this as const up here and I think that should do the trick so we'll save that and run and you can see it prints both of those values so with this context you should Now understand what's going on here we basically have the first argument being the count very similar to this and the second thing being the array of character arrays so that's the first scenario where you might encounter a pointer to a pointer the other scenario where you're going to have a pointer to a pointer is if you want to be able to change a pointer value inside of a function to illustrate this we're going to get rid of everything else we have here we're not going to need it we're not going to need the parameters to find there either so let's just go through an example where we can create a pointer so you'll often get a pointer if you use new so let's say we're working with an INT pointer called data and this is going to be a new int with the value 5. inside of C programming it's going to be very similar except you don't have the new operator so you could instead use Malik and pass in the size needed and then we'll go ahead and output the value of this by dereferencing that pointer and we should get the value 5. so running this and you can see the value 5 in the terminal so let's go ahead and create a function void modify data and this is going to take an into pointer we'll also call it data no big deal and inside of here what we're going to do is we're actually going to assign it a new value so the value add data is going to now be 10. and then down here we will invoke modify data passing in the data pointer so we'll save we'll run this code and now we get the value 10. so this works we can change the value that a pointer points to no big deal you can see we're printing that outside of the function call and it retains the value 10. but if there's a scenario where we actually wanted to change where a pointer points to it's going to be a little bit different so what that might look like is inside of our code here we could create a new pointer so we'll just call it int pointer Temp and we'll say new int and we'll give it the value 10. and now what we could do is just assign this to data but to do this what we can do is actually delete data so we're not going to need the previous pointer just as a extra little bonus point there and then last thing we'll do is we will assign to data temp and let's just see what happens when we output this so we will dereference data and put a new line so now we have two outputs one outside of the function and one inside of the function when we run this the first one to be printed is actually inside of the function so this line is hit we set data to five we invoke modify data inside of modify data we replace that pointer two points somewhere else and then we print the value add data and you can see it prints 10. but then after that function finishes and we come back to line 13 we output data it still has the value 5. so if you wanted to be able to change the pointer itself where actually points then you're going to need a pointer to that pointer anytime you need to basically change data you need to add one layer of pointers so that's why if you wanted to just change data inside of a function and have it change outside you would need a single pointer if you wanted to change that pointer you're going to need a pointer to a pointer same thing if you were working with three levels of pointers or four levels of pointers but don't ever do that so to fix this all we need to do is to find this as a pointer to an into pointer and this is going to change the way we type some of this other stuff now we can change the pointer by first dereferencing and removing the old pointer from memory similarly here when we assign a new port pointer we will dereference to work with that first layer pointer and then when we invoke modify data we'll actually pass the address of data which is going to be used to create a pointer and now when we save and run we should get 10 and oh well the output here is messed up so you can dereference this twice since we're working with a pointer to a pointer now we should get 10 10. so here's a small example of where you might encounter this if you had a linked list and a pointer that pointed to the start of that list and you wanted to create a function to change the linked list such as putting a new element at the front of the list to be able to change that start pointer you're going to need a pointer to a pointer this is going to allow you to modify the start pointer to point to the new data I know this code is completely out of context so you might not understand it all but there are various scenarios where you're working with pointers and you want to abstract away some complex code so you move it to a function but because you moved it to a function in order to actually work with those pointers and change values you now need to work with a pointer to a pointer so while we haven't covered every possible scenario where you might encounter these we gave a couple of examples and gave you the foundation of how they work so you should be able to take these principles and apply them in various locations let me know in the code comments in What scenario you ran into working with a pointer to a pointer and if you got any tips to make it easier besides just not doing that but thanks so much for watching and stay tuned for the next video
Info
Channel: Caleb Curry
Views: 7,743
Rating: undefined out of 5
Keywords: pointers to pointers, pointer to pointer, pointer array, string array, double pointers, c++ pointer, c++ pointer to pointer
Id: 9aqdsVWuWf8
Channel Id: undefined
Length: 13min 18sec (798 seconds)
Published: Thu Apr 27 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.