hi everyone welcome to my channel in this video i want to talk about pointers so i will go straight to the point uh pointers are very important concept in c plus plus and they have many uses in z plus plus now pointers are not limited only to c plus plus they exist in other programming languages as well but in most of those programming languages they are really hidden they work behind the scenes so you cannot see them and you cannot use them yourself but it's c plus you can use them and you can create your own pointers and they can be pretty powerful if you know how to use them but they can get quite confusing as well if you don't understand them well so the first thing that i want to explain is what are pointers so we already have explained the concept of a variable and you should be familiar with that so we said that a variable is just a container so a container that stores certain value now a pointer is container as well but instead of storing value pointer stores an address so a memory location and let me show you how pointers work in visual studio let's create a variable of type int and i'm going to call it n and assign it a value of 5 and let's write out this n like this so nothing new here we are already familiar with this and if i run my program as you can see we get this value of five so that was expected now as i already said this n is a variable and that means that it is a container which is storing certain value now because it is a container that means that it has its address inside memory so it has its physical location so how can we get that location how how can we check which address this n has so in order to do that you use this ampersand symbol like this so you put it before your variable name and that should give you the address of that variable so if i run my program now as you can see now we have an address um and this is the address of our n variable so this is its physical address where this value of five is stored and because this is pretty hard for humans to remember this is just a random numbers and characters because of that we use meaningful names and we access these values that we store in our memory using these meaningful names which are our variables so i'm going to close this now and what i want to show you here is i want to show you how can you create a pointer that is going to hold this address of our n variable so in order to create a pointer you give it a type first so you say int and then in order to indicate that you are creating a pointer you use this star symbol and then you give it a name so let's call it ptr pointer and i'm going to assign it the address of our n variable so i'm going to copy this and paste it here okay now our pointer is holding the address of our n variable and in order to prove that i'm going to write out the value of my pointer like this so as expected we have this first line here which is writing out the address of our n variable and then this second line of code is this line here and as you can see we have written out the value of our pointer and that is the same address as this one here which means that our pointer really is storing the address of our n variable now you may ask how can i access or can i access the value that is stored on that address using pointers and the answer to that question is yes you can and in order to do that in order to access the value that is stored on this address that your pointer is holding you have to dereference your pointer so how do you do that well let me copy this so as you already have seen this line of code here is going to write out the address and in order to write out the value on that address you dereference the pointer and in order to do that you add this star symbol before your pointer's name so if i run my program now as you can see in this last line of code we have the value of 5 which is actually the value that is stored on this address here so let me show you one more thing what i want to do is i want to change the value that is stored on this ptr address so on this address that our ptr pointer is storing so how am i going to do that i'm going to say again star symbol and then ptr and this here means please access this memory location so whatever i'm going to assign here is going to be stored on that memory location and what i want to assign is for example value of 10 like this so now if i copy this line of code and then paste it here and if i run my program again we have this value of 10 stored on this address here and one interesting thing as well is that if we try to write out the value that our n holds now like this if i run my program as you can see our n holds the value of 10 as well even though we have never said that n has the value of 10 but we have stored the address of our n in our pointer and then we have dereferenced our pointer meaning we have changed the value on that address here in this 11th line of code here so that's why our n holds value of 10 now one very important thing to keep in mind is that your pointer has to be of the same type like the variable that it is pointing to so let's close this so as i said this integer pointer is pointing to an integer variable so our n is of int type and the same way would be if we created a float pointer it would have to point to a float variable and then char pointer can point only to a char variable double bool uh and so on so pointer and the variable that that pointer is pointing to have to be of the same type okay so that means that if i try to change the type of this n variable to float for example like this so if i say float immediately we get an error here and the error says a value of type float pointer cannot be used to initialize an entity of type endpointer meaning you cannot assign address of a float type to pointer of int type okay so that is the error that we have here and i'm going to return this to int okay and as you can see that error has disappeared because now our pointer is pointing to the same type so to the variable of the same type that that pointer has so int pointer pointing to an int variable one thing that beginners very often try to do is the following so they say okay let's create a pointer so int let's give it a name ptr2 so pointer like this so i have created a pointer right so let's dereference this pointer and assign it a value so i'm going to say please dereference ptr 2 and assign it a value of 7 for example now this code here has a probe problem and if i run it if i say build solution actually as you can see it says uninitialized local variable ptr2 used this means that this ptr2 so this pointer to does not have an address so where should it store this value of 7 if it does not have an address and the easiest way right now to solve this problem is going to be to create a variable and to give the address of that variable to our ptr to pointer so i'm going to say int v this variable has to be of the same type as our pointer as we already said and then i'm going to say here our ptr2 is going to hold the address of our v variable like this and now we shouldn't have this problem anymore so if i build my code again as you can see one succeeded so the problem has disappeared and let me write out the value of this v variable so i'm going to say v is equal to and then let's write out the value of this v and if i run my program as you can see it says that v has the value of seven because we have assigned it here by dereferencing our pointer that is holding the address of our v so you may say now okay salina i understand this but this seems a little bit too much why would i create a pointer just to assign a value to a variable and the answer to that question is you don't this is not the type of problem for which pointers were created in c plus plus so there are different problems that pointers solve in c plus plus and this here is just a demonstration of what are pointers and how can you create your own pointers now examples of some of these other problems that pointers solve is you can use pointers in order to pass values by a reference to a function and then you can use them to return multiple values from a function which is an interesting one you can use pointers in combination with arrays as well you can use them for dynamic memory allocation and then if you are familiar with oop object-oriented programming you can use a pointer of a base class in order to access a object of derived class and then there is this concept of smart pointers which we are going to cover in the future so i believe as well that i have mentioned some of these things in some of my videos but i plan to make a dedicated series two pointers where we are going to talk only about pointers so stay tuned subscribe to my channel um hit that bell icon as well so that you are notified when i publish my next video and if you like this one give it a thumbs up thank you for watching and i'm going to see you in my next video where we will be talking about pointers bye