 Hello everyone, I am Mrs. Savita Palival. In this video, you will learn how array elements can be accessed with pointers. In previous videos, we have discussed basics of pointers and arithmetic operations on pointers. By now, you might have understood how to use pointers in a program and what arithmetic operations are possible on pointers. That student will be able to write a program to access elements of an array using pointers. The prerequisites for this lecture are basics of pointers and understanding of array in C. In C, the elements of an array are stored in a contiguous memory location. For example, if we have array integer arr8, 1, 2, 3, 4, 5, 6, 7, 8, these are the values which are assigned to this array. Then, this is how the elements are stored inside the array. Here you can see the first element is at address 3000. Since, each integer takes 4 bytes, the next element is at address 3004 and so on. So, these addresses are 4 bytes. They are having the difference of 4 bytes 3004, then 3008, 30012 like this. This program demonstrate that the elements of array are stored in a contiguous memory location. You can see here. In this program, we have declared an array of size 4 to which we have assigned some values, 10, 20, 30, 40. There is a for loop which is going to iterate for 4 times. Now, inside this for loop, we are attempting to print the elements of array and we are attempting to print their addresses also. So, here you can see array %d. %d is a format specifier for integer. So, it is going to print 10. In the beginning, in first iteration value of i is equal to 0. So, arr of 0 that is 10 will be printed over here. Then, we are printing its address. Address of arr0. So, for printing the address, we are using format specifier %u because address is an unsigned integer and we use %u format specifier for unsigned integer. So, this will print the address of a 0th element in an array, ampersand arr0. Now, in this way, this loop will iterate 4 times. So, for every element, it will print its value and for every element, it will print its address. So, this is the output of the program. Now, you can see here, element of an array occupies 4 bytes. So, each subsequent address would be 4 bytes apart from its previous address. Therefore, here you can see 12, 3, 3, 5, 0, 0, 12, then 16, then 20, so on. But these memory addresses may differ every time you run the program. Now, we need to fix one thing in our mind about an array that the name of array is a starting address of the array in memory. That is, the address of first element in an array. So, this starting address of an array is known as a base address, starting address. So, just now we have seen the array which is going to start from the address 3000. So, 3000 is a base address of this array. So, we can conclude that the name of the array is actually a pointer that points to the first element of the array. Therefore, now you can see this array. For this array, if we print, base address of array is %u arr, means what are we using here? We are using here the name of array, arr. So, if we use name of array, it is going to print you the base address. So, this will print 3000. This is the output. Similarly, another print up statement, base address of an array is %u ampersand arr0, means address of 0th element. Okay, address of operator ampersand, address of 0th element. So, this will also give you 3000. So, these are the two ways to print base address of an array. Now, let us use pointer variables in which we can store the base address of an array. Here, we have declared two pointers, integers star ptr1 and star ptr2. Inside this ptr1, we are keeping the base address of an array. That is what the address of a 0th element of an array. Alternatively, what can be done? ptr1 is equal to arr, simply the name of array. So, either we can use name of array or we can use ampersand arr0, name of array 0th index. So, this will assign the base address of the array that is 3000 to the pointer variable. Now, here you can see the ptr1 is right now consisting of address 3000. Let's take another pointer ptr2. If we make ptr2 to store address of the fourth element. Now, here the index is 3, but actually it is a fourth element of an array. Okay, because indices of the array starts from 0. So, this is a third index, means it is a fourth element. Fourth element's address is 300012. Therefore, ptr2 will consist of 300012. Even you can make ptr1 to contain fourth element's address or fifth element's address. So, what will happen? It will simply replace the previous address with new address. Even you can do ptr1 is equal to ampersand arr5. So, it will store 3020 inside ptr1. Now, as we said name of the array is actually a pointer. Means what? It points to the first element in an array. So, we could assign name of array to ptr. What can be done? ptr is equal to arr. ptr is an integer pointer. Arr is an integer array. So, base address of the array we are assigning to the integer pointer. So, this can be done. Now, think for a while and give answer of this question. Can we write arr is equal to ptr? What does it mean? Can we assign a pointer variable to an array name? Let us consider few lines of code for the reference. One array of size 4 to which we have assigned some values 10, 20, 30, 40. There is an integer i. There is an integer pointer star ptr. ptr is an integer pointer and star ptr we have declared. Inside this ptr variable we are keeping the address of a variable i. Then we have written arr is equal to ptr. So, ptr is right now holding the address of i. So, i's address we are attempting to put inside arr as it is also an pointer. It is also a pointer. So, my question is whether this statement arr is equal to ptr is correct. Let's see the answer. No, we cannot assign a pointer variable to an array name because an array has been already declared. So, its memory has been allocated and location where the first element of an array will be stored cannot be changed now. With this statement we are trying to change the address of an array. That is not possible. Therefore, array name is often known as constant pointer. What is constant pointer? Constant pointer means pointer that we have once assigned. With some address we cannot now change its content. Means we cannot change the content of that pointer. So, that is what is a constant pointer. Therefore, array name is a constant pointer. So, arr is equal to ptr is not a valid statement. Now, let's see how array elements can be accessed using pointer. In our previous discussion, in the previous videos, we have seen that increment or decrement operators can be applied on pointer variables. We know that a pointer when incremented always points to an immediately next location of its type. If it is an integer pointer, then after incrementing four bytes, then after incrementing four bytes are added in the current address present in the pointer variable. Because it is an integer pointer and integer size is four bytes. Similarly, if ptr holds the address of the first element in an array, then ptr++ will store. So, this particular statement will make the changes in the value of ptr. So, now ptr will store the address of successive element of the array inside it. Now, let us see the program in which we are trying to access the elements using pointers. Again, in this program, we are assigning some values to the array arr, it is of size four, one integer pointer, one integer pointer ptr, one integer variable i. Inside ptr, we are storing the base address of an array with the statement ptr is equal to arr. So, this is a base address. We are putting this base address inside ptr variable. Now, inside this for loop, again, the same thing we are going to do, we are going to print the elements and we are going to print the addresses of these elements. So, arr% means here arr0 is equal to star ptr. Star ptr means value at address stored inside ptr. So, value at the address stored inside ptr is 10, because right now it is consisting of the address of the 0th element. So, it will print 10. Then this is going to print the 0th element's address because we are printing the value inside ptr. Value inside ptr is a address of 0th element. So, it will print that. Then we are doing ptr++. ptr++ will now will contain the address of the next array element that is address of 20, because it is getting incremented by 4 bytes right now. It will contain the address of 20. Then in the second iteration, it will print 20 and its address and so on. So, every time whenever we increment the ptr, it is going to contain the address of the next element and we will print that element. So, this is how we can access the elements of the array using pointers. This is the output you can see, the same output you will get. So, every address is 4 bytes apart. These are my references. Thank you.