What are double pointers in C?

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
I want to take a look at what is a double pointer today in this video so we have here a simple string just a string basically a pointer to a set of characters these characters are stored somewhere in the global memory and we just get a pointer to that now I want another function it says void display this guy takes in a double pointer to charm but I want a pointer to the string that means I have to have a double pointer to char here and I'll call it altered just so that we don't mix the names and I'm gonna call it s the other and all this guy is going to do is just print F percent aspects of n the output itself that we pass in here and then we can just call this function right display of the address of STR because it is a double pointer it is not just a simple point on and here when we print F well since output is actually a double pointer to char basically a pointer to a string well we have to dereference it once okay now if I run it I'm just going to get on this screen here this is a test but what does this double pointer syntax actually mean let's take a look at a little more detail so here right let's let's type in the string first right we have the string somewhere in memory fills this is a test putting that all itself this is a test this is the string itself and think of this sheet of paper as the actual memory we're looking at and this guy is stored somewhere in memory right here let me get a paintbrush this is inside our memory now we have our STR I'm going to type it in here our STR here is this STR inside the main I said a main function that is a pointer so what that STR is is basically a variable a variable that stores some data that data represents the address of this string all right so I'm gonna type in here 0 X 0 1 let's say that let's say that's the address but of course it's not gonna be just that but suppose that this is the address to our string right and this in turn also has an address right so this is somewhere in memory STR has the value this which really can get a bigger printer here which really points to this guy right but this guy also has a memory address right so we can have something that points to this because these two are really the same thing they're just kind of blocks of memory okay and this is where we have that output so this output here is actually this pointer so if I go here and say output it is actually this pointer that points to our STR value so here let's say 0 X I know 0 F let's see so these are just made up addresses by the way don't don't actually take them seriously but just so that it's easier to write so this in turn this is the value write a value inside a place in memory that represents the address of STR of the value of STR and then the value of STR is as well another address that points to something else that points to a string in this case now we can really ask ourselves what is STR so STR is a address right it's an address to something so it's a pointer a pointer to what what are we actually pointing to well this T here is a character so it's a charge pointer simple as that so output then is a pointer to this guy ready like we asked before what is tea tea was a character in our case output was an address that pointed to something what is that something well that something is a sharp pointer so we have a pointer to a chart pointer so what double pointer to child and that's the type of our variable here now of course you can keep on going with this you can really get another variable that points to the address of this output because remember everything that we have here this this string here is somewhere in memory this address here right this pointer is also somewhere in memory and this double pointer as well is actually somewhere in memory so we can get another pointer to it if we wanted to and we can keep on going forever and ever right but this is what it actually means to have a double pointer is just another pointer to another pointer so when I say here okay I want the address of STR the annals of STR is of type well double pointer and that I am assigning to our output here well this guy is a double pointer to chop now why would it make sense to have a double pointer at all well that's the interesting part when you have a double pointer it's actually kind of nice that you can really change the value of the pointer you're pointing out so right here we have output that is a pointer to our STR and STR is a pointer to our string Toto string itself so what we can do is dereference out of ones if we dereference it once only once not twice we are gonna get well actually STR the STR inside the main function and we can say give it a different value how about this is another test okay hmm what's going to happen then well if we for example printf this value so after the call and % s and then STR here if I run it you'll notice that before the call this was just this is a test and after we called it this guy actually changed so this is another test so really we were able to physically change this guy which is a pointer this is what double pointers are usually used for if you have a pointer that you want to change at some point but you want to change it in another function you have to get a double pointer you have to have a pointer to a pointer because dereferencing that double pointer once here well what does it mean well it means taking this address from output and looking at what's in there well what's in there is actually STR right so this STR the STR that is in main this is what we're getting and we are assigning it a different value we're saying point to something else so we change this to for example 0 X I don't know Fe or something like that that points to another to another string so here this guy it's probably gonna point to I know something else something like this is another test write a string and as you can see these two these two strings are different strings they have different addresses in memory but we were able to change it to just by actually using this double pointer we we had a pointer to this STR then we can change it if we have a pointer to it and by changing it we physically changed that pointer to point to some other value now technically you can of course have a pointer to a pointer to a pointer to a pointer till infinity if you want I can observe here we go you have a pointer to a pointer to a pointer to a pointer and so on and so forth like 8 times or maybe 9 but this is a bit tricky to use and it is kind of useless usually it is useless sometimes it's actually useful the trickier part about this is that if you add how many of these like for 9 so these are 8 or 9 pointers right one after the other and at the end we have the actual string so here we will need those pointers right and just adding a get address of this like 9 times or 8 times I think it is 2 3 4 just 8 times doesn't actually work doesn't actually work but isn't it supposed to give me the address of an address will not actually work not quite like remember this diagram right we have the output here this is a physical variable that itself has a has a has an address right I marked out just by just drawing this drawing these lines alongside it but when we try to type in the reference of the address of STR this guy should have the same values of 0 X 0 F in this case but it doesn't actually have an address so well that's simply because of how operators work right in what you're telling the program to do is to get the address of STR it doesn't tell you to store it anywhere just kind of plops down there right pops it in the CPU to try to call another function with it or do something with it later on you're supposed to store it again so when you try to get the the address of the address of STR well there's simply no address there this guy doesn't actually have one address it's just a value right because at some point the program what it does it just goes well address off str okay sure other stuff str is when your x0 after right and this is a value so once it turns into a value it you cannot get the address from just a value so when we did something like this well again address of str turns into a value so 0 x is 0 f and then this doesn't make any sense now let alone all these here so try ever getting a valid address of a value the value itself doesn't have an address it's just value that you use in the program values are not stored intrinsically in the program so trying to do this is not okay so you cannot actually get the address of the address of the address of the others like that you're going to have to actually create each pointer one after the other so you would really have to do something like this well as you can see we really have these many pointers we really have in memory in this function these many pointers inside the functions stack right so STR would have an address STR - would have an address which represent the address of STR which has the address of STR STR 3 has the other surface T 2 and so on and so forth up until 9 which I just kind of pass along to this guy which is like 9 pointers to Chara however you want to call it usually again this sort of technology doesn't exist because these are not that useful but technically it is possible so you shouldn't fear double pointers to charm right just pointers to another data type that happen to be pointers and yes you would be able to actually dereference them multiple times so here we have when we had just a double pointer so that we don't complicate ourselves even further you could technically dereference the output twice what would that result in - well since output is a double pointer to char dereferencing it twice well what does it mean if we go here well the reference in output ones would get us to this guy right so it would be this guy and inferencing it again would give us this so it would just actually give us this letter here right the first character of the string so if you do your fancy twice you actually get the first letter of the string which you cannot actually change by the way well that's another story so similarly here when you have an output it has nine asterisks here you can dereference it nine times if you so desire so you can dereference it as many times as you have pointers right so a simple pointer can be referenced once a double pointer can be dereference twice a triple pointer can be delivered as three times and so on and so forth and that's really all there is to it I hope you got something out of this video if you do have any questions do leave them down the comments below or on our discord server thank you so much for watching take care bye you
Info
Channel: CodeVault
Views: 7,983
Rating: 4.9283156 out of 5
Keywords: pointers, double pointers, c (programming language), codevault
Id: jUcqT37FdUI
Channel Id: undefined
Length: 14min 43sec (883 seconds)
Published: Wed Apr 15 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.