What is the Difference Between Pass By Pointer and Pass By Pointer Reference (int * and int * &) C++

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everybody this is Paul in this video I'm going to be explaining the difference between a pointer and a pointer reference so I've already gone ahead and I've written some code here to save us some time in the tutorial and I also started a diagram of some of the things I've got going on in this program so the first thing I've done is I've created an integer variable called box 1 and that's represented by this guy right here I said that box 1 holds the value 1 so let's go ahead and put a 1 in here okay so there's our one and then box 1 lives at some address and we're just going to say that that's address a this address is actually determined when your program runs but we're just giving it some sort of name that we can reference for illustrative purposes the next thing I do is I create a new integer variable I call it box 2 and I place the value 2 inside of box 2 we're saying that box to lose that address B and inside of box 2 we're going to place the value - okay so box 1 holds the value 1 and box 2 holds the value 2 the next thing I do is I create this pointer called G pointer and so that guy's right here here's G pointer and currently we haven't assigned him anything to point to the next thing I do is I declare a function prototype so this passed by pointer function is the one I'm going to call to demonstrate what happens when we pass a pointer into a function and this function passed by pointer reference is what I'm going to use to demonstrate what happens when we pass a pointer reference into a function so see the difference here is in this case I'm passing in an int star for an integer pointer and here I'm passing in an int star and purse and for a integer pointer reference alright so let's go ahead and jump to the main program here now inside the main program we create an integer pointer we call it P and we make it point to box 1 so what we're doing here is we're saying store the address of box 1 inside of the pointer P so when we place an address inside of a pointer that effectively makes the pointer point to the thing at that address so what this line right here is doing is it's creating this pointer P right here and it's saying put the address of box inside of P and so that means we take this address right here we place it inside of the pointer P which effectively makes PE point toots box 1 so that's what our program looks like at this point the next thing we do is we say store the address of box 2 inside of G pointer so G pointers right here box 2 has the address B so we're going to place be inside the G pointer and that effectively makes G pointer point to the thing at address B and the thing at address B is box 2 so here's what the diagram looks like for our code up to this line right here the next thing we're going to do is we're going to just print this title to the screen and later in the tutorial we'll take a look at what happens when we pass P inside of the pass by pointer function and we'll also look at what happens to P when we pass it into the pass by pointer reference function but for now we're going to ignore this and we're just going to look at the current conditions before we pass P into either one of these functions the next thing we're going to do is we're going to compare the thing that's inside of P which is an address and we're going to say if P contains the address of box 1 that means that P is pointing to box 1 otherwise if P holds the address of box 2 that means that P is pointing to box 2 and if neither one of those are the case then that means that P is pointing to another box so we don't expect to ever see this print in this tutorial but it's good to have it here just to make sure we're covering all of our bases the next thing we're going to do is we're going to look at G pointer if G pointer contains the address of box 1 that means that G pointer is pointing to box 1 otherwise if G pointer contains the address of box 2 that means that G pointer is pointing to box 2 and if neither one of those are true that means that G pointer is pointing to another box which we're not going to expect in this tutorial but we're including this line just to make sure we don't get something unexpected and then the next thing we're going to do is we're going to print the value inside of box 1 then we're going to print the value inside of box 2 so without calling the pass-by pointer function and without calling the pass by point of reference function let's just take a look and see what this prints out here so we'll make sure this file is saved in here got my terminal open to the folder where this file lives right here I'm just going to compile it real quick by saying G + + and the name of the file is PTR and I'll just push tab and it will autocomplete for me I've named this file pointer versus pointer reference TPP and that's what this file is here that I have open so I'm going to compile it real quick I do an LS we see now that we have our code file in this directory plus we have this new executable that was created from compiling here now let's run the executable dot forward slash a dot out and this is what we see P is pointing to box 1 okay that looks right here G pointer is pointing to box 2 that looks correct as well box 1 contains the value 1 that looks good in box 2 contains the value 2 so that looks good as well so now let's jump back to our program here and this time we're going to uncomment this line and what we're doing now we're passing P which is this guy right here into the pass by pointer function so let's go to the pass by pointer function and see what happens and so when we pass it in by pointer right here what happens is is we passed in P but it actually creates a copy of P named PTR in this case so let's update our diagram to see what this looks like as we enter into this passed by pointer function so we pass in P what we actually get is we get a copy of P so we get a new pointer and it's named PTR and PTR basically is a copy of fief it contains the address a so we put the address a in here and that effectively makes PTR point to the thing at address a which happens to be box 1 so this is what our diagram looks like once we enter the pass by pointer function by passing P into it now we have this star PTR which is the dereference value of pointer and that means the thing that pointer is pointing to make that have a value of 3 the thing that the pointer is pointing to is box 1 so box 1 gets the value 3 so box 1 now has the value 3 then the next thing we do is we say make pointer equal G pointer so what that saying is make pointer contain the same thing that G pointer contains well G pointer contains the address of box two so we're going to put that inside of PTR here and effectively what that does is it causes PTR to now point to the thing at address B which is box to the next thing we say is the thing that PTR is pointing to this is the gear reference value of PTR make that a four well PTR is pointing to box two and we're going to make that equal four we're going to print that the pass-by pointer function has been called so let's go back to the terminal let's clear the screen and we'll make sure our program saved I've already done that and then we'll recompile the program so I'll just push up a few times and get back to my compile instructions I'll push enter and then I'll run the executable dot slash a dot out so here we're printing the current conditions we can see that we've gone through the pass by pointer function and we can see that P is pointing to box one so that's this guy right here P is still pointing to box one G pointer is pointing to box two so that's this guy so he hasn't changed box one contains the value three okay that looks correct and box two contains the value 4 so we can see that we haven't changed the thing that P is pointing to and we haven't changed the thing that G pointer is pointing to either so we have effectively updated the values in box 1 in box 2 in our past by pointer method so let's go ahead and get rid of this guy and let's see what it looks like when we pass by pointer reference so going back up here now we're going to comment the pass by pointer call-out and this time we're going to pass PE into the past by pointer reference I'll save this and then we should probably update box 1 and box 2 to be what they used to be since this time we're not calling the past by pointer function we never get the 3 and 4 in these boxes that get set by that function call here so going back the only thing in this run when we compile it is going to be box 1 contains 1 and box 2 contains 2 so let's update that ok so the only thing we're doing different with this run is we're calling the past by pointer reference call this time and we're passing in PN P happens to be pointing to box 1 and we're not going to change any of the output here so let's go ahead and take a look at happens in the past by pointer reference call so what we have here is we're creating a reference to the pointer we're passing in by adding this and person so what this is doing is instead of making a copy like the past by pointer did we're creating a reference to the thing that was passed in so the reference just happens to be a pointer and so in this case since we passed P in we now get this PTR F as a reference to this same pointer so essentially what happens here we pass P in and we get this new way to access the pointer here so we can use this pointer and we can use it by calling P and our main program or in this function call we can now use it as PTR ref so the difference here is this pointer was a copy of the pointer passed in whereas this pointer right here is the same thing that we passed in so now we're actually manipulating the same pointer that we're calling P so this is saying put the value five into the thing that pointer ref is pointing to so this is the dereference value of pointer ref we're saying make the dereference value of pointer refs five to the dereference value once again you do by star and then the name of the pointer that means the thing that the pointer is pointing to make that value in this case five so that's what happens here now we say pointer ref equals G pointer so whatever's inside of G pointer put that inside of pointer ref G pointer contains the address B which is where box two lives so we're saying put the address be inside of pointer ref so now pointer ref contains the address be and that's where box two lives so effectively this makes pointer ref point to box two and it just so happens that since pointer ref is just another way to call this pointer that also means that P is now pointing to box two so the next thing we do is we say make the thing that pointer ref is pointing to contain the value six the thing that pointer ref is pointing to is box to box to now gets the value 6 and then we print that the pass by pointer reference function has been called so let's make sure our file is saved again go back up here just to double check that we've got the passed by pointer reference being called and we do so that files saved let's go back to our terminal clear the screen go back to our compile command enter run our executable and we see that this time we've called the past by pointer reference function it says P is now pointing to box two so that's this guy right here so he has updated now to be pointing to box two G pointer is pointing to box too okay so that guy hasn't changed box one still contains the value five okay that's what we expect in box two now contains the value six which is what we see on our diagram as well so the main difference between passing by pointer and passing by pointer reference is that when we pass by pointer reference if we modify that the thing that the reference is pointing to we're actually modifying the pointer we passed in that's what we see in this last case however if we go back here we can see that when we just passed it by pointer we make a copy and then at the end P is still pointing to box 1 and G pointer is pointing to box 2 since the past by pointer was just a copy P is still pointing to the same thing that it pointed to before the function was called whereas when we pass by pointer reference as we see in this diagram we can actually modify the thing that the pointer points to one of them we pass by pointer reference so anyway that's the big difference between passed by pointer and a pass by pointer reference thank you guys for watching feel free to suggest more topics that you'd like me to cover have an excellent day and if you haven't already don't forget to subscribe
Info
Channel: Paul Programming
Views: 49,691
Rating: 4.9731126 out of 5
Keywords: What is the Difference Between Pass By Pointer and Pass By Pointer Reference (int * and int * &) C++, what, is, the, difference, between, pass, by, pointer, and, reference, c++, cplusplus, pointers, int *, int * &, paulprogramming, paul programming, paul, programming, tutorial, tutor, lesson, computer science, function, call, address, pointer reference
Id: 7HmCb343xR8
Channel Id: undefined
Length: 12min 38sec (758 seconds)
Published: Sun Nov 06 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.