Hello and welcome everyone to this practical module of C programming course…..
And like before, we will be discussing a new practical, which will basically help you to revise the arrays and pointers concept.
So, without any delay we will quickly go and see what will be the today's practical will be related to which topic and in which way these topics are helping us revising these topics.
So, we are talking about our today's practical.
It's name is printing array elements and addresses using pointers.
Now, we had made normal arrays and we also had made normal pointers in the entire course.
But if I talk about, I want a combination of arrays as well as pointers.
Now, what does this mean? This means that this problem statement is telling us that you do one thing, you make one array and the address of each and every element of array as well as the element that has been stored inside that array, that information which is there, the address as well as the value of the element on the array, these two things you print for us.
With the help of what? With the help of the pointers.
So, here we are going to discuss how the array’s element’s addresses as well as its data, how those will be printed.
That we will be discussing inside this practical.
So, without any delay we will go into our text editor and we will see how we can use it and make a new program.
So now we have come inside our text editor and you can see that we have made one basic template for our C program, for which we have already written our preprocessor command and as well as our main function.
So, now we will go ahead and see what will be the first step that we will be doing in this program.
Since we have to work on the arrays, along with that we also have to make one pointer.
So, definitely we will have to define one array as well as one pointer.
So, for that we will make “arr” named array, whose size we have kept here as 10.
So basically, we will be able to store here 10 elements in our array.
Now we will go ahead and see our next data type variable, what is that? That is our pointer.
Now we have made here “poin” named pointer.
So, how do we get to know that this is the pointer, it's very simple.
We have learned it early on, at the time of declaration of any variable if the asterisk is placed before it.
So, we will be declaring one pointer over there.
Along with that we will see further, we have also made one i named variable and because of the previous programs you must be the recalling that I, j these kinds of single lettered variables which are there, these we usually use in for loops.
Since we are talking about the arrays and the input of the arrays we will take it through the user so definitely we will be needing for loop over here.
That's why we have defined i named variable.
Now after defining all these arrays and pointers, now we will go ahead to write our main logic.
What will be our main logic inside this program? We know that our ampersand symbol is used for what? So, that whatever thing is written in front of it, we can access its address, that on which location it is stored that we are able to access, that's the reason why we use this ampersand symbol.
Even here you can see that on the 0th index position of the array, the block, the element, its address has been accessed.
How? By using ampersand symbol and where have we stored it?. We have stored it in our pointer which means our POIN named pointer, in that which address we have stored? We have stored the address of array’s first element, the element on the 0th index position, its address we have stored in the pointer.
So, here we will quickly understand the logic that why are we storing it? Now what we will do is we have told our pointer that array’s starting position or the starting address, the location is this, with the help of this logic.
Now we also know that array is a continuous memory allocation, because of a the next address, the next element’s address, it will definitely…whatever is the data type of our array, its next address location will be there.
That's why inside the pointer we need only have to store the first address location.
The further ones we can access by using for loop.
So, this was about our logic, how the address of our array’s element, we are giving to the pointer.
After doing all these things, we will be going ahead and we will be making our array, which means the 10 size array that we have declared, in that we have not put even one element for now.
And what we are doing for putting that element.
We are making use of for loop, how will this for loop work? We will see that over here.
In for loop, one by one the array’s data, the values that we have to store in our array, that we are taking from our input screen, which means we are taking those values from the user.
That's the reason why we have made a for loop over here.
So, the for loop is somewhat this way.
We have used i for iteration, for repetition.
And I we have initialised with 0, after initialising it with 0 we will see, since going ahead we have put the condition of i > 10.
So, our this loop will work only 10 times.
Why? Because i’s value is first 0.
After this the i’s value will become 1.
Since we have put the increment operator over here.
So, from 0 it will be 1, from 1 it will be 2, from 2 it will be 3 and, in this way, when it will go up to 9, till that time this condition will keep getting satisfied and our for loop will go on.
But as soon as in our condition i’s value becomes 10.
You will see that our condition will become false.
Why? Because i’s value is 10 and 10 is not less than 10, 10 equal to 10.
That's the reason why this condition will not be satisfied over here.
And our loop will only run for 10 times.
So, this was about our loop’s iteration.
Now we will go ahead and see, what are the things that we will be writing in loop.
So, in the loop we will be writing one printf statement which will tell our user that which element it has to input, that's way in printf, we have written “enter element”.
After that we have put the format specifier “%d” but along with that since we want to display the double digit values.
That's the reason why we are making use of 02 over here.
We remember that when we used to deal with float numbers or with the double numbers.
After the decimal we wanted a specific number of values only, at that time what we used to do? At that time, we used to write the values like .02, .3.
So even here we have to represent two-digit elements.
Since we have 10 elements that's why we are writing 02 between our format specifier.
Now we will see ahead what we have printed? We have printed “i+1” value.
You all must be knowing that here i we have started from zero.
So definitely, when we tell anyone to enter the first element, we have to tell first, we will never say 0th element.
So, to represent the first what we have done is i’s value is starting from 0, so that we can declare it 1, we have written here i +1.
So, if the i’s value is 0, then 0+1, which means it wants to print first element.
So this was about the printf statement, the printf function.
Now we will go ahead and see our scanf function.
Even in this we will see that what we have done is we have put “%d” format specifier but here you will see that in front of the pointer we have written +i, why have we written that? We have written that because we are not storing the values directly in the array.
What we are doing is in the pointer the array’s location that we had kept.
We're taking that location further, as we know that the integer’s sizes is 2 bytes, in the same way, the next two byte’s one will be the address location in which the array’s second element will be stored.
Because of this we are keeping on increasing the pointer’s value.
This doesn't mean that we are storing the values inside the pointer.
The values are stored inside the arrays but we are accessing it through the pointer because in the point we had put the arrays first element’s address.
As soon as we will do +I, i is one integer value.
After doing +i what happens, our pointer goes on to the next location.
And it starts pointing towards it.
When it points it, through scanf, we store that value on the particular element.
In the same way one by one till the loop goes on, our pointer keeps on going ahead.
One after the other, it will go on to the next location because array is a continuous memory allocation.
That's why the address locations will be continuous and the next ones will keep on coming.
Now the work has been done, we have stored the array’s value by making the array, with the help of pointers.
Now the time comes to know how we have to print the values and address, we will be seeing that over here.
For that what we will be doing here is “enter array elements are” we will write this kind of printf statement and ahead of that we have used one more printf in which we are telling that in this line we are putting the address of the array elements.
And further to it we have written the value, so address and values are two different things.
That's why in between them we have to define some gap.
And with what we have defined that gap, you can see that we have defined it with \t, \t means tab.
The space equivalent to tab will come in between these two.
As soon as we use \n to change our line in our output.
In the same way we use \t, so that we can give gap in between two things on the same line and how much is that gap? It is equivalent to the tab which means of four spaces.
Here 4+4, 8 spaces we have put.
Since we have written \t twice over here.
After that we will see that we have put one more for loop again.
And what are we doing this time? This time the conditions are completely same.
The i’s value is starting from zero and it is going on till less than 10, which means our for loop will also go on for 10 times.
But you will see that our printf statement which is there over here.
It is a little different.
What are we printing over here? We will see that, first of all you will see that % x is the one format specifier that we have used,. It will be used to print the address over here.
In between that we have put 08, this means that the value that this format specifier will print, how big will it be? It will be 8 digits big.
That's why we have written 08 over here to display our address our location.
You can put any values over here 0 8, 0 10.
So, in front of our location 00 will come.
So, in this way we can limit our digits over here.
Along with that you will see that we have put \t and we have printed one more value, that actually is our %d, which means the value that we have stored in the array.
That exact value we will be printing over here, even that we are printing in three digits.
That's why we have written here 03.
So, first our %x format specifier, inside that POIN+i, what is the address location of that particular location, that we are getting printed over here.
Along with that we have put the asterisk mark over here and we know that when we put the asterisk mark before the pointer, the value that accesses, it's not a location, the value that is stored on the location that is being accessed over here.
So, what we are doing basically with the help of this printf statement, we will be making two columns.
One would be for the address and other would be for the value.
First in the address one, with the help of this we will be printing addresses from 1 to 10, whichever are our array’s limits and corresponding to that the values that are there, we will be printing them here with the help of Asterix symbol.
So, this is our entire program, code.
How this should work and how this is working.
How along with the values of array we are printing the address of the array with the help of a pointer.
All these things we have done here in our program.
Now, the time comes to execute this program, to run this program and to see how our output is coming out.
For that what we will do is we will run the program.
As soon as I will run it you can see over here that slowly my compiler is compiling it and you will see that here I'm able to see some error.
So, we will see ones what exactly is the error… So, like I had told you before.
Here sometimes our compiler gives us some issues.
So that's why what we will do here is we will execute it once again and see.
So, what we have to do is, first we have to open the terminal and whatever has happened in the terminal till now, we will clear that then we have to write our command but before that, we will open the command prompt over here.
So now we will write our command over here which is gcc, along with that, whichever is our files name.
So, my file’s name is “readarraywithpointer”.
As soon as I write it over here, you can see that this has been compiled successfully.
Now what we will do to run it, we will press one character and you can see that our program has been run, now what is the output that the program is giving, we will see that.
This says, “enter array elements”.
So, we have made a printf statement over here, you can see that, enter array elements because of that, it is telling us that you enter the arrays elements.
So, you will see that the first element number 01, we have to enter.
So, I will enter 5 and press enter.
In the same way, it is asking us to enter the second element.
What I'll do is I will quickly enter all the elements after that we will see how our output will come over here…..(coding)so we have entered 10 elements over here and you can see that as soon as I enter the 10th element, after that with the help of printf statement, statement got printed.
So, this you can see that here the addresses and values that we have entered, we are getting it as an output over here.
So, you can see that the first element that we had entered was 5.
So, 5 has been stored on which location, on which address? So, it has been stored in this particular address.
In the same way our second element was 078, it has been stored on to this address location.
And this kind of address locations kept on going ahead and as and when we had put the values the address location kept on going ahead.
So, this was about how the addresses of the arrays and the elements of its values, we can print it with the help of pointers.
So, you will see that here the space is a little more.
So, what I will do is I will reduce the space.
Instead of shifting all these elements further.
And here we will see that in between the values we have put the tab only once but here for the title we had put two tabs, so that's why we were getting to see more space over here.
Now what we will do is we will compile it again and run it.
Now I will put the values over here….
it has got a little bigger.
So, here in this way…..(coding)
so now you can see that now it is completely aligned.
And how our addresses, values have been printed.
So, now you can see that our values are printed in eight digits, as we had specified here that we want the values only in eight digits.
So this was our program of arrays and pointers in the same way we will be meeting you in the next video in which we will be seeing similar kind of a new problem.
If you think that there is any part of this video or this topic, you might have not understood then you can ask us any query or question related to that topic without any inhibition.