In this article, we will get to know how we can access an array through a pointer with help of some example.
As we have learn earlier that pointers are special variables that are used to store addresses of another variables. Pointers can not only store address of a single variable but it can also store the values of collection of similar types variable i.e. Array.
Pointer doesn’t store addresses of each and every cell of an array it only stores the address of the first cell of an array.
Consider this example :
int *p; int arr1[10]; // store the address of the first // element of arr1 in p p = arr1;
Here, p
is a pointer variable while arr1
is an int
array. The code
p = arr1;
stores the address of the first element of the array in pointer p
.
Notice: that we have used instead of
&arr[0]
. This is because
the name of the array always points to its base address so it is not required to use address of
operator(&) with the array name . So, the code below is the same as the code above.
int *p; int arr1[10]; p = &arr1[0];
Suppose we need to point to the third element of the array using the same pointer p
.
Here, if p
points to the first element in the above example then p + 2
will
point to the third element. For example,
int *p; int arr1[10]; p = arr1; // p + 1 is equivalent to &arr1[1] // p + 2 is equivalent to &arr1[2] // p + 3 is equivalent to &arr1[3] // p + 4 is equivalent to &arr1[4]
Similarly, we can access the elements using the single pointer. For example,
// use dereference operator *p == arr1[0]; // *(p + 1) is equivalent to arr1[1] // *(p + 2) is equivalent to arr1[2] // *(p + 3) is equivalent to arr1[3] // *(p + 4) is equivalent to arr1[4]
Suppose if we have initialized p = &arr1[3];
then
// p - 3 is equivalent to &arr1[0] // p - 1 is equivalent to &arr1[1] // p + 1 is equivalent to &arr1[3] // p + 2 is equivalent to &arr1[4]
Note : Adding and subtracting in a pointer is totally different as compared to adding or subtracting in any kind of value. When we add.
But when we add 1 in address (pointer) then compiler calculate the increment by multiplying the added value with the size of data type of pointer.
Thus if:
***The above sizes of data types are of turbo compiler sizes may vary in other compilers.
Example: C++ Pointers and Arrays
// C++ Program to display address of each element of an array #include <iostream> using namespace std; int main() { float brr[6]; // declare pointer variable float *p; cout << "Displaying address using arrays: " << endl; // use for loop to print addresses of all array elements for (int i = 0; i < 6; i++) { cout << "&brr[" << i << "] = " << &brr[i] << endl; } // p = &brr[0] p = brr; cout<<"\nDisplaying address using pointers: "<< endl; // use for loop to print addresses of all array elements // using pointer notation for (int i = 0; i < 6; i++) { cout << "p + " << i << " = "<< p + i << endl; } return 0; }
Output :
Displaying address using arrays: &brr[0] = 0x61fef2 &brr[1] = 0x61fef6 &brr[2] = 0x61fef10 &brr[3] = 0x61fef14 &brr[4] = 0x61fef18 &brr[5] = 0x61fef22 Displaying address using pointers: p + 0 = 0x61fef2 p + 1 = 0x61fef6 p + 2 = 0x61fef10 p + 3 = 0x61fef14 p + 4 = 0x61fef18 p + 5 = 0x61fef22
In the above program, we first simply printed the addresses of the array elements without using the pointer variable p
.
Then, we used the pointer p
to point to the address of brr[0]
, p + 1
to point to the address of brr[1]
, and so on.
// C++ Program to insert and display data entered by using pointer notation. #include <iostream> using namespace std; int main() { float prr[3]; // Insert data using pointer notation cout << "Enter 5 numbers: "; for (int i = 0; i < 3; i++) { // store input number in prr[i] cin >> *(prr + i) ; } // Display data using pointer notation cout << "Displaying data: " << endl; for (int i = 0; i < 3; i++) { // display value of prr[i] cout << *(prr + i) << endl ; } return 0; }
Output :
Enter 5 numbers: 2.5 3.5 4.5 Displaying data: 2.5 3.5 4.5
Here,
prr
.
cin >> *(prr + i) ;
This code is equivalent to the code below:
cin >> prr[i];
Notice: that we haven't declared a separate pointer variable, but rather we are using the array name prr
for the pointer notation.
As we already know, the array name prr
points to the first element of the array. So, we can think of prr
as acting like a pointer.
for
loop to display the values of prr
using pointer notation.
cout << *(prr + i) << endl ;
This code is equivalent to
cout << prr[i] << endl ;