Interested in Personalized Training with Job Assistance? Know More
My name is (name) and we are continuing with this C programming course.
In the last video, we had discussed pointers, we saw exactly what the pointers are and after knowing all these things, now is the time to perform a few operations on pointers.
Till now, we saw how the pointers are made, how they can be used but now, we are going to see how operations are performed over the pointers.
So, we will go ahead and start with a new topic which is called ‘pointer arithmetic’.
If we talk about our maths, in maths, we must have learned that we use arithmetic operators as operators.
In fact, when we were completing this C programming course, we saw in operator’s topic that there are different types of operators and one amongst those is ‘arithmetic operator’.
In which simple mathematical operators are used like our addition, subtraction, multiplication, division and these kinds of operators which we use in maths, the same operators we are using here in C programming and what do we characterise those in.
These are called arithmetic operators.
If we talk about this topic or if we talk about pointer arithmetic, with this topic we understand that on the pointer, some operation is going to be performed and how will that operation be, it will be of arithmetic type like addition, subtraction or this kind of operation, those we are going to perform.
So, it is extremely clear and simple that on this pointer we are going to perform operations like addition, subtraction and we will see how its values are changing.
So, without any delay we will start and first of all, which means before starting the operations, we are going to make one pointer and over that we are going to perform the operations.
So, here we have got a simple code, a simple program where we have considered only the main function.
So, here on the screen you can see that we have started with the main function, here the very first line in our main function is array declaration.
So, in the array we have seen that in the square brackets of the array, we write the size of the array and if we have to declare its value, we put an equal sign in front of it and put values in the curly brackets.
Similarly, in the same way we have declared the array over here whose data type is integer and along with that the size is five.
Since the size is five, how many elements would be coming in the array? Five elements.
So, we have declared here five elements along with it, which are 12, 45, 23, 67 and 44.
So, as you are able to see, here we were talking about the pointers, but we declared the array.
So, as and when we will go ahead in the program, we will understand why we are declaring the array over here.
So, here we have declared the array of size five and we stored five elements, whose index position has been started with zero.
So, 0, 1, 2, 3 and 4.
So, the last element which was there, whose index position was left out, is 4 and the first element in the array, its index position is 0.
So, this was about the index positions.
Now, we will move ahead and we are going to declare our pointers.
So, here we will be declaring two pointers and why have we considered arrays, we will see its answer over here.
In this particular program, what are we going to do? Inside our pointers, we are going to store the address of the value of any of the array’s elements.
So, in this way the flow will move, from the five elements of arrays which are there, among those any two values which are there, their addresses would be stored in our P and Q pointers.
So, here we can see that we have made P and Q pointers, which are of integer type, which means it must be pointing the integer values and how did we get to know that these are pointers? because of the Asterix mark in front of it, with that we got to know that we have declared the pointers.
So, we have made P and Q, two pointers.
Now, we will go ahead and see how we will be storing values inside them.
So, the array that we made, its name is ‘ARR’.
So, whenever we will be willing to extract the value inside it or if we will be willing to access it, at that time we will use this array's name.
In the same way, you can see here, inside the ‘p’ pointer, the value of the ARR array at the 0th index position.
We have stored its address over here.
Which is the value at the 0th index value?
It is 12.
As we know that index position starts from zero and zero means the first element, our first element is 12 in the array.
So, the 12 in the array stored at whatever location, inside our computer, its address would be stored in p and how did we extract this address, this address we have extracted from the ampersand symbol.
Which we have put before the array here.
So, you can see due to ampersand, our address which is there, array of index position zero, whatever value there is, its address with the help of ampersand, we have extracted, and stored it and assigned it, in p pointer.
In the same way, if we talk about the q pointer, even here the value at the third index position of the array, we have stored its address in q.
Which is the value on the third index position? That is 67.
Why? Because if we start the index position from zero.
So, 0, 1, 2 and 3, so on the third index position, 67 has come.
So, this thing is clear about the pointer declaration, how we have made the array and now on the elements of array our pointers are pointing.
So, this was about the program code.
But if we want to understand it visually, how exactly it works.
Point means exactly what is it pointing to? How is it pointing at? So, we will see that in the next slide.
And from that it will be easily clear, how exactly these pointers work.
You know that our arrays are in chain structure.
The chain structure exactly means that its address i.e. the location of each block is contagious.
Which means, if at any one location, our array’s block is getting stored, then the second block just adjacent to that, will also be stored in the next storage location.
So, in this way, the chain of our arrays which is made continuously, that chain basically is stored like one after the other the addresses that come.
So here we can see for visualisation that we have made an array of five blocks.
We made a chain, as our array was of size five.
So, we have made only five boxes / blocks here and you can see we saw in the last side that we have stored the five values, those five values are stored somewhat in this way, if we talk about visually, the first index position.. here on the top you can see on the top of the blocks, we have written the index position for every block.
So, you can see the 12th value is stored on the index position zero, in the same way 45 is stored on 1. Similarly 23 is on the second index position, 67 is on the third index position and 44 is on the fourth index position.
So, this was about the index position and our values.
How we have given the index position to each value so that when we are considering that value with the help of this array, so with the help of that array, we could access that value on that particular index position.
So, this is about an index position.
Now, if we talk about the values that are stored, for the sake we say we are storing it in the array,but if we talk about how they're stored inside of a computer, in the computer it is stored simply in the form of bytes.
We have seen that our computer understands a binary language, which means it understands zero’s and one’s language.
So, whatever data is stored in it, is in the format of zero’s and one’s and we call zero’s and one’s as bits.
So, these bits come together and become bytes and bytes come together and become gigabytes, megabytes, terabytes and these kinds of different terms, we give it to these bytes.
So, you can see it here.
This array represents the storage location in our program but inside the computer this value is stored in some of the other addresses.
And how are these addresses? These are the continuous addresses.
And in this there is one important thing, if we talk about our arrays, which type of value is stored in our array.
That is of integer type and we all know how many bytes an integer occupies, two bytes. right?
So, if we talk about the array, we all know that the first block will take the two bytes and the next block will take two bytes, in the same way we will make each block of two bytes in the integer type array.
So even here if this is an integer, here each block’s size would be 2 bytes.
So, as an example, if we say that the value is 1000 of the address location of a particular block.
What will happen with it? The next block will be 1002.
This we are just taking for our reference, the values written below 1000, etc these are all dummy values, we have written it as per our wish but in the computer these values are automatically generated based on the space or on whichever space these values are stored, here for our reference we have given them the values like 1000, 1002, 1004, 1006 and 1008, these are the values that we have given, we have given such locations or such addresses are given to all these blocks or to these values.
The logic to give them, we will quickly understand it here.
You can see that one block is of two bytes size.
So, our first block which is there if its location is 1000.
And if you see the array is going in the adjacent way, which means there is a continuous storage of it, which means if we have stored the block of two bytes, the next block would be stored at the next adjacent storage space and even that will take two bytes.
In the same way the value of the first block, its address location is 1000.
The next block will be two bytes ahead.
So, that will be our 1002 and in the same way if we are adding the third block.
In which location will it be? It will be on 1004, in the same way 1006 and 1008.
So, what is happening here is, we are increasing our address location by 2 bytes, as we know that our blocks are of two bytes, since they are of integer type.
So, here there is no need to go so deep as the values here are dummy.
We just want to understand that our locations or addresses are stored based on bytes, which means the first element of the array is stored in this particular location.
So, the second block that will come will not be stored here or there.
It will just be stored at a side storage location.
And after this, if we want to take this address.
So, if in the same address, we add 2 bytes then our pointer comes on the next block.
So somewhat in this way our location and addresses work.
So, the pointer which we had declared in our previous slide, which means p and q pointer.
Where was the p pointer declared? On the 0th index position and q on 3rd index position.
So, if we see it visually, you can see here that p is pointing towards the 0th index position, which has
the value as 12.
And inside that, the value that is getting stored.
Here it is 1000, for reference.
And if we talk about our second pointer, it is pointing towards the 3rd index position, which means on the 67 value and the location in it or the address has got stored which is 1006.
So, this was about a pointer declaration and visual representation, how these pointers are pointing over the array.
Now we will straight away go ahead and see if we perform any operations over these pointers, does their values change or do they don't.
Even if they do change, how they change, all these things we are going to see here.
So, we will move ahead and see about the very first operation, which means our pointer increment operation, we will see and learn about it.
So, you all know that this plus -plus operator, increment operator, what does it exactly mean, when we were implementing in our normal variables, our increment operator, which means the Plus-Plus operator.
At that time what used to happen, whatever value was stored in the variable, in that, +1 used to happen.
Suppose if I did iPlus-Plus and earlier the i’s value was five.
So, what will it be now? the value of I will become six.
In the same way our normal variables which were there, if you see according to them, our increment operator meant that adding one, in what? In our value.
But if we talk about our pointers, here the concept is a little different.
Well, The basic concept is the same.
But the way is a little different.
So, we will understand it here, how it works.
If we say that p is our pointer.
It is pointing at a certain value.
This thing is clear but when we put an increment operator on it.
It logically means that on whatever value the pointer is pointing at, point it on its next value.
So, here if we see an example, the pointer that we had declared.
Where was the p pointer pointing at? It was pointing at 12 value.
If we make it P-Plus-Plus, this will start pointing at the next value which is 45.
So, this works in this way.
As we do pPlus-Plus, instead of pointing at 12, it starts pointing at the next value which is 45.
If we want to see how the value is, the address that is getting stored, if we see how, it is getting changed.
So here we will understand that first we were pointing at 12 values.
And its address or location was 1000.
If we have to point at the just adjacent value.
So, we all know that in the address, the continuous values are getting stored.
So, its address or location will only differ by 2 bytes, if it is an integer type of array.
So, by the same logic if the value stored in p is 1000 and if I make pPlus-Plus, which means if I put a pointer increment operator.
So, the value inside it will increase by two bytes and it will become 1002 and on1002 whichever value will be stored.
It will start pointing to it.
So, in the same way if our array was of a float type, we would have considered 4 bytes difference and our pointer would automatically store 4 bytes ahead address into itself.
In this way pPlus-Plus meaning logically means pointing at the next value but since the values are not of single bytes.
If there is an integer here that means it is double byte, which means it is two bytes, if it used to float, it would have become 4 bytes.
In the same way, as many bytes there are, according to that we change the address.
So, this was about our pointer increment.
By doing p Plus-Plus, p will point at 45 and the value which will be stored in it will be 1002.
Now, we will move ahead and see exactly similar operators like this one which is our pointer decrement operator, it works in the same way, it walks on the same concept.
Here if we consider q pointer and if we perform decrement operation over here.
You will understand why it was earlier at 67 and the value stored in it was 1006.
But when we do q minus minus , q will now point at 23.
It will point at 23.
But to point at 23, what should be the value stored in it? The location of it should be 1004.
So, even here also, it will reduce its value inside by two bytes.
Why? Because we are talking about integer arrays.
So, in this way in P- Plus-Plus we are pointing at the next values and in q minus minus, we are pointing at previous values.
So, this was about our increment and decrement operators.
Now we will move ahead and see that I don't have to point at just the next value, I want to point at the next two, three values.
In that situation, how will we work? We will see that.
Here, what we have done is here, we have considered ‘k’ as the constant.
And let's suppose we have kept the k’s value as 2.
Now 2 is inside k.
And if we talk about the 1st expression over
Here, what exactly they are asking here is to add p in k.
K is 2, so what is it that we are basically trying to write here? p+2.
Like in our previous slide, in the previous example on doing pPlus-Plus, we had pointed to the just next value.
So, the p+2 that we are doing, in that we are pointing at the next two values.
Which means leaving one we will be pointing at the next value.
In the same way if k’s value was 3, what would we have done? We would have left first and the second and we would have pointed to the third value.
So, this is the basic concept of our constant addition.
So, if we follow this expression and keep the k’s value as 2.
Then the p will not point at 45.
It will straight away go to point at 23 and in that the value will be stored 1004.
Similarly, if we talk about the second expression, here it is q-k, if k is 2, q will go back by 2 values.
And instead of pointing at 23, it will straight away point at 45.
So, this was about our constant addition and subtraction.
So, we thought increment and decrement and along with that if we want to add any constant on my pointer, how it is done, till now we have seen this much and learned it.
Now, we will see one more topic quickly.
This is addition or subtraction, which means the two pointers that we were using, if we add and subtract them together, what will happen then? At that time basically what will happen is, if we see as per the example what have we done? q pointer minus p pointer, and whatever value will come we will be storing it in I.
So, if we talk about q-p, what happens exactly in this.
The q and p pointer which is there, in between how many values are there? If you want to know that, at that time we perform this kind of operation.
And here we can see in the example if we do q-p, so here in between we can see 2 values, which are 45 and 23.
So, in total there are 2 values between q pointer and p pointer.
So, that's why the output of the value will be 2 and 2 will get stored in i.
In the same way if we do addition and by doing the addition, we can make out how many values are there in between and if we do the subtraction, we can get to know that between q and p pointers, how many values are stored.
So, we've talked about operations, how the pointer’s operation should be performed and after doing this operation how the values inside the pointer changes or wherever the pointer was pointing at, how were they changing.
So, we talked about pointer arithmetic concepts.
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.
Now in the next video we will talk about one more topic, which is called array of pointers.
There we will be making pointer’s array.
What is the need of making the pointers array? This we will be seeing in the next video.
Till that time, you practise the pointer arithmetics and we will meet you in the next video.
Thank you so much.
Have a friend to whom you would want to share this course?