Hello everyone, my name is (name) and we are continuing with this C programming course in which in the last video we had discussed Arrays.
Arrays are one type of derived data type and we saw that there are different types of arrays, one is single dimensional and the other is multidimensional.
Then we saw how the single dimensional array makes the chain and stores the values one by one.
And now, we are going to discuss a second derived data type which is called pointers.
In this video we will see why they are called pointers and what is the use of pointers.
Do we really require one more data type or the arrays were sufficient? All these questions we will see in this video and we will see how we have to make these pointers and use them in our programme.
So that we can make our program more efficient.
So, without any delay we will start and give an answer to a very easy question which is called what are called pointers.
Now, what are these pointers? We will read the definition once, pointers are the variables that contain the address of another variable within the memory.
What does this mean? It means that a pointer is a type of a variable like we used to initialise and declare the normal variable, in the same way we initialise and declare pointers and this is a normal type of variable, but in which we've stored the address of some other variable's location.
Now, we have seen that our storage space in which we have whatever values stored, that is stored at one particular address and there are different addresses of all the values.
So, what does the pointer do? The pointer receives the address of the value stored in some other variable and it gets stored in our pointer variable.
So, this is basically used to store the address, that's why it is called one type of variable.
So, we will see how its syntax will be.
So, for syntax you can see, first of all we have to write data type and after that we can see a start symbol and after the symbol there is one pointer name that is written.
So, we will understand what is the exact functionality of these three.
Now, we have learned that our pointer is also one variable, that's why the value that is stored inside it, that will be one address of some other variable, right?.
So, this another variable that will be of which type? So, suppose the other variable is of an integer type.
So, our pointer in which the address will be stored, its data type will also be integer.
So, the type of variable that we will store in our pointer, that type of data type will remain.
So, this was about our data type.
After that, we will see what the meaning of the star mark is.
Its syntax is basically like a normal variable, how will our compiler know that this is not a normal variable that we are making, but we are making a pointer.
So, to create that differentiation between a normal variable and a pointer variable, this asterisk mark is used.
And with this asterisk mark, we get to know that here a pointer is being made.
After an asterisk mark, we will give a pointer name which we all know that, the way we have to name a variable, we'll also have to name the pointer, we give the name and after that we put the semicolon because of this, the declaration of our pointer gets completed.
So, here we got to know that the value that is stored in some other variable, the value where it is stored, the address that we are getting from there, that address is stored by our pointer.
Now, we will go ahead and see how this pointer can be used and what are the steps and procedures to use these.
So, the first step would be defining the pointer variable.
Now, since we want to use the pointer, we will have to make a pointer variable.
So, we have made a pointer variable.
After that, what is the step? After that is “assign the address of the variable to the pointer”.
So, whichever variable’s address we want to store in the pointer, so this assigning process will come in step two, after defining the pointer.
Now, we have also assigned the value in the pointer.
Now, what is the third step? “we have to access the value at the address available in the pointer variable”.
Now, this is a point to concentrate on, the value which is there inside of a pointer variable, it is basically an address has been stored.
So, this address will be on some storage location in which the value is stored, right?
So, how can we use this so that the value which is stored on that address can be accessed with the help of the pointer.
So, this third step is talking about it.
Now we will go ahead and see how these three steps are fulfilled with the help of our program.
Before going ahead, we will see one more thing, in which way if we have declared the pointer, after that the initialization part, which means the part where the address is stored.
How does that work? Here is a simple example, we have made a normal variable with the name role, which is of integer type and in it seven is stored.
On the look of it, seven is stored in a variable.
But if we talk about storage space, it has gone to some location and has been stored over there which has a specific address.
That address is basically What we are going to store in our pointer.
So, we have made a normal variable and we have come ahead after coming ahead you can see that there is one pointer that has been made over here because we have used one asterisk mark has been used over here and its data type will remain as an integer because whichever variable we'll be storing in the pointer, its type will be integer itself.
So, we'll have to keep the pointer’s type as integer.
So, now we have written I N T, then we have used the asterix symbol, after that, we have written the name of the pointer.
So, this is the declaration part, if we talk about initialization, so what has happened in initialization, after using the equal to sign we have put that variable’s name R O L L, it means roll and in front of it, we have also put one and symbol, which we also call as ampers and.
And we had read this before, after using this ampers and, the address of that variable is referred to by the compiler.
So, we had also seen this in scanf function, in which way to store that particular address, we make use of ampers and.
So, even here the roll variable, what would be its address to get to know that, we will put an and, as soon as we put and, the variable will be converted into address and that address will go and get stored in our pointer.
So, these are the steps of declaration and initialization.
Now we will go ahead and see how we can make use of these pointers in real life in our program, so that's why we will go and see in our text editor, how we can use our pointer.
So, here comes my pointer’s program.
This is one simple program in which we will get to learn a lot about the pointers.
So, now we will start with the first line, the preprocessor command line.
After that we will come on to our main function and after the main function starts our initialization of the normal variable.
So, we have made a normal variable named VAR in which we have assigned 20 value.
Now if we come on to the second line on the second line, we are making one pointer which is called PTR, which has a data type of intq because we will be storing var address in it and VAR’s data type is also integer, so that's why the pointer’s data type will also be integer.
And how we will get to know that this is a pointer, with the help of this Asterix symbol.
So, this is our declaration part.
If we see ahead, with VAR we have also put one ampers and, which will extract the VAR’s address for us and that address will be stored in the pointer.
So, this process is the process of declaration and initialization.
Now we will access our variable’s address.
So, first of all, we'll be using one printf statement here.
And with the help of this printf statement, what we will be printing? We will be printing the address of our variable directly without the use of pointers.
So, what have we done here, since we have to print the address and not the value of the variable.
To print the address we know that we will be using ampers and.
So, that's why we have used the ampers and over here.
So that the variable’s address will get it printed over here.
The first line will print the variable’s address for us, without the use of pointers.
But if we talk about the other printf statement, here you can see the address stored in P T R is… we have written somewhat this type of statement and what is happening over here, we are printing the address stored in the pointer.
So, now in the point we have stored the address and if we only write the name of our pointer, the address that is saved inside the pointer, the compiler will get it over here for us.
If we write only PTR, that is only the pointer’s name, then what will we get, the address that we had stored of whichever variable, we will get that address printed.
And now we will run it and see then we will understand this properly.
Now we will see the third printf statement, this says the value of VAR stored in PTR.
What is happening over here, we saw that at a certain location the value was stored.
In var whichever variable was that, in that the value was stored or assigned.
And that was getting it stored at a certain address, am I clear?
So, here there are two things, first would be to access the address and the second would be with the help of the address, we could access that value as well, right?
So how we will be able to do this, if we only want the address, so we saw that the step is straight away writing the pointers named.
It will give us The address that was stored in that pointer.
But if I want to see the value that has been stored on that address, I want that and I have to bring that.
So, I will have to put the asterisk symbol in front of the pointer.
So, this thing is clear over here.
If I only want the address, we will put the pointer’s name without the Asterix symbol.
And if we want to know what are the values stored on that address, we want even that then we will put Asterix in front of our pointer’s name because of which our compiler will know that this time we don't want the address, but it has to give the value stored on the address.
In this way the program will go on.
So, we will now run this and see what will be our output.
Here our program has started running.
And if you see what is the output that has got printed, so the output which has got printed is these three statements which are saying, the address of VAR is 6 1 F E 1 4.
So, what is this basically, this is the address on which we are basically storing those values.
So, we know that the first printf statement was printing, it was printing VAR’s direct address.
VAR’s direct address is 6 1 F E 1 4.
Now, we will see with the help of the pointer, what is the address? So, now, we will see with the help of the second statement if we use the pointer to store the address, is the address changing? No, you can see here that the address stored in P T R is 6 1 F E 1 4.
So, the address is in this format itself wherever the value is getting stored.
That's the reason why both of these are the same.
So, if you are directly using the ampersand and getting the address printed, even that will be the same.
If we store the address in the pointer and get it printed.
What is one more benefit of the pointer, along with this address, we want to see what are the values stored on it, if you even want to know that then you can put a star before the pointer’s name, asterisk.
And as soon as you put that, you can see that in the third statement, the value of V A R stored in P T R is equal to 20.
So, we have seen in the program that in V A R we had store 20 and storing in var means storing in one particular storage location, which address we know.
So, this address, we come to know with the help of ampersand and we store it in our pointer and if we want the address we straight away right the pointer’s name and we get our address and if you want to know what is the value stored over the address.
Then we put the asterisk mark in front of the pointer’s name because of which we get that particular value.
So, somewhat in this way our pointers work, which means along with it the addresses that are saved or the values that have been stored on that address, we can perform all these things with the help of the pointers.
Along with this it so happens that on one pointer, another pointer is being used, which means we have saved a normal variable’s address inside one pointer and that pointer’s address we have stored in some third pointer.
In this way, pointer to pointer, we can perform different things with the help of pointers.
So, this was about the basics of the pointers.
What is the meaning of pointers, how are they used? All these things with the help of this example or this program, we've seen.
If you have any queries or comments, click the discussion button below the video and post there. This way, you will be able to connect to fellow learners and discuss the course. Also, Our Team will try to solve your query.
Share a personalized message with your friends.