Pointers in C

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello my name's Gary Sims and this is Gary space and one of the advanced features you find in the C programming language is the idea of pointers now a lot of people struggle to understand pointers but once you master them you really can do some very advanced things in this language so if you want to find out about C and pointers then please let me explain okay so what I'm about to tell you is value in a course for a standard C it also works in C++ and it works mainly the same in c-sharp now normally when you have a variable in C programmer let's call it X and you want to add one to it the compiler knows to have a place in memory for variable X it might fix that value it can add one to it and then write it back out into that place in memory now a pointer is a bit different it's not the variable X it's a pointer to the variable X in other words it holds the address in memory where variable X is stored now once you have the address of where X is stored you can follow that pointer or dereference that pointer and then manipulate X by let's say adding 1 to it you can also change the value of the point of itself to point to different places in memory and this is really powerful when you're pointing to an array because you know the inner array all the different elements are 1 next to each other so you can move the pointer from one element to the next element by just moving it along in the memory so the best way to really get to grips this is for me to show you an example so let's go over to my PC we're going to type in two short C programs that show you the power of using pointers in C okay so here I am on a Raspberry Pi and let's just go into Nano and we're gonna write a program called pointer dot C we'll just start with the normal things you need in a C program so we have an integer main and then we'll go into our program okay so normally if you were in a C program you might have for example like this you might have integer is equal to seven and then we might do a printf you know something like X is and then you use percent D to get the decimal the number out and then we might print out X okay and then of course we could do something like say X is equal to 14 so that we could see its changing and we could do it again you know X is percent D and then we would know that what program would change so let's just quickly run that and see what happens so we can do a GCC pointer dot C so that worked and then we can run the program X is 7x is 14x actly what we're expecting but now let us define a pointer to X so the way you do that you say int because it's a pointer to an int and we say star star tells you it's a pointer a pointer and it's going to be equal to where well the we want to point X but how do you do that will you put an ampersand in front of you say the address of X and that tells you that a pointer now points to the address of X and if we print it out a pointer we can do that because we're not expecting the address of X we can so here we can say a pointer is and now I'm going to use % X because I want the number to come out in hexadecimal which is a bit easier to read and so if I now put here a pointer okay we're not gonna get the value of x we're gonna get the address where X is stored in memory so let's just try and compile that and let's run it and here you can see a pointer is be9 9 0 2 4 0 so that's an address in memory where the point where X is actually stored we didn't know that before we knew the value of x we knew at the 7 we knew it changed to 14 but now we know that it's actually stored at this address B e99 0 to 4 0 so let's now continue here and now what we can do I just boiler splint a pointer with an either but that doesn't really matter okay now what we can do is we can change the value of x by using the Poynter in fact let's do something a little II read the value of x before we change the value of x so we can say X is and we're going to do percent D again so we getting the value of x but now we're going to use pointer so we know it points to X okay so how do you actually get that to read the value put a star in front of it so ampersand tells you want to get the address of and star tells you want to follow it or dereferences so now that should show X is 14 so let us just compile that and run it and here we get then you see X is 7 X is 1400 is the address of the pointer and now X is 14 now notice B a 5 4 - 4 oh that addresses going to be different every time because you don't know the guaranteed address where it's going to be when the program actually runs so let's go back into our editor and now we can actually change the value of x by actually saying star a pointer so we're saying the value of dereference it wherever it points to follow that pointer and change it to 21 so that's what we're going to do and now all we can do two things we can say printf X is and we're gonna do % D and then % D again and the reason I'm going to do that is to show that one time we can just say X and we're expecting 21 and the other time I'm going to say star a pointer which means it follows the pointer and gives us the value of it so let's just compile that and run it and there we can see X is 21 21 once by using the value X or 1 by using star a pointer which is dereferencing the pointer to the place where X is and you also get 21 okay now we're going to do a second program and we're gonna call this array pointer dot C and again we just quickly go in and do what we need for C so we need a main function ok so what we're gonna do this time is show you how you can do some pointer arithmetic so let's say we have an array of numbers some and it's 5 long so it's a standard C array of some numbers and now what we're going to do is we're gonna create a pointer so we put a star okay and we're gonna call it we want it to point to some numbers now the interesting thing is in C and array name is a pointer to itself now that would be the equivalent of doing ampersand some numbers 0 now you don't need to do that because an array name is actually a pointer to itself so we now have a pointer which points to the beginning of the array and now I'm gonna use a variable V because I want to fill this array with something so I need something to have some values in it so what they do now is this we can say have a for we don't need to initialize anything because the pointer is already pointing to the beginning of the array and we say keep going while pointer is less than some numbers 5 and remember the array goes from 0 to 0 1 2 3 4 so 1 is less than the 5 and of course we need an ampersand in front of that because the address of all that so why the point has not yet reached the address of what would be element 5 which of course doesn't exist now here's the trick bit pointer plus plus you can increment the pointer and what that means is the pointer will move on to the next element in the array and the great thing is is it does it exactly by the array size so if you did characters it would be different it would move a different amount if you did num integers if you did long it knows exactly how far to move along now we can say start pointing to follow the pointer and set it to V so in this case the first time around it was edited too and then just to give us a different value each time to say V is equal to V times V times 2 just to just to change it and that's it and now that we'll fill the array with 2 4 8 16 and 32 and then to check that it's done that we can now do this the more traditional way we can start with I and we can say for i is equal to 0 i is less than 5 io + + your standard kind of way of going around an array and we can just say printf percent the backlash in to get the character line and then we're gonna say some numbers I assume 0 1 2 3 4 print out those different parts of the array and a closing bracket there let's see whether this compiles GCC array pointer yes it did now let's run it 2 4 8 16 and 32 so they're being printed out using the traditional way of going through an array index 0 index 1 index 2 and we filled it out by using pointers and moving the point is along the array so there we have pointers in the array and we actually have the traditional way well I really hope you enjoyed that video now if you do like programming videos I highly recommend going over to digit academy.com because over there I have a programming course that you can sign up for which deals with Java and Android app development so if you want to learn how to do Android app development if you want me to teach you how to do Android app development go over to digit academy.com and I'll see you over there well my name's Gary Sims and this is Garrick space I really hope you enjoyed this video if you did please do give it a thumbs up you know what I'm gonna ask you please subscribe please share this on social media and please feel free to use the comments below to ask me any questions you have about pointers and I'll try my best to help you ok well that's about it I'll see you in the next one
Info
Channel: Gary Explains
Views: 241,293
Rating: undefined out of 5
Keywords: Gary Explains, Tech, Explanation, Tutorial, The C programming language, C/C++, Pointers, Dereferencing, Value of, Pointers in C, Pointers in C++, Pointers and Arrays, Addresses, Pointers and Addresses, Address Arithmetic
Id: mw1qsMieK5c
Channel Id: undefined
Length: 10min 9sec (609 seconds)
Published: Tue May 29 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.