Before we move on to know how pointers work on an array? Pointers to an array you will know in-depth communication between array and pointers. Before starting pointers to an array first, let me know the basic concept of pointer and array.
What is Pointer?
Pointer is a variable that stores the address of another variable.
// General syntax
datatype *var_name;
// An example pointer "ptr" that holds
// address of an integer variable or holds
// address of a memory whose value(s) can
// be accessed as integer values through "ptr"
Example - int *ptr;
Example of Pointer:
// C program to demonstrate use of * for pointers in C #include <stdio.h> int main() { // A normal integer variable int Var = 10; // A pointer variable that holds address of var. int *ptr = &Var; // This line prints value at address stored in ptr. // Value stored is value of variable "var" printf("Value of Var = %d\n", *ptr); // The output of this line may be different in different // runs even on same machine. printf("Address of Var = %p\n", ptr); // We can also use ptr as lvalue (Left hand // side of assignment) *ptr = 20; // Value at address is now 20 // This prints 20 printf("After doing *ptr = 20, *ptr is %d\n", *ptr); return 0; }
Output:
Value of Var = 10
Address of Var = 0x7fffa057dd4
After doing *ptr = 20, *ptr is 20
What is Array?
An array is a collection of similar types of elements.
//Syntax
data-type array-variable[size];
//Example:
int a[5];
Now see an example:
//Here we initialize an array variable and Display it #include<stdio.h> int main() { int a[5] = {1,2,3,4,5},i; printf("Elements inside array:\n"); for(i=0;i<5;i++) { printf("%d",a[i]); } return 0; }
Output:
Elements inside array:
1
2
3
4
5
After recalling array and pointer now is the time of pointers to an array.
Consider the following example:
In C++:
#include <iostream> using namespace std; int main() { int arr[5] = { 1, 2, 3, 4, 5 }; int *ptr = arr; cout <<"\n"<< ptr; return 0; } // thus code is contributed by shivanisinghss2110
In C:
#include<stdio.h> int main() { int arr[5] = { 1, 2, 3, 4, 5 }; int *ptr = arr; printf("%p\n", ptr); return 0; }
In this program, we have a pointer ptr that points to the 0th element of the array. This program returns the output of array first element address means base address,
0x794758ABC
If we access all array elements addresses then we get the next array element address by increment ptr++ the code will be,
#include<stdio.h> int main() { int arr[5] = { 1, 2, 3, 4, 5 },i; int *ptr = arr; for(i=0;i<5;i++){ printf("%p\n", ptr); //increment ptr++ to move on next array element ptr++; } return 0; }
The above code returns all array elements addresses in a consecutive manner.
Output:
0x7ffc51294160
0x7ffc51294164
0x7ffc51294168
0x7ffc5129416c
0x7ffc51294170
Now if I want to access array elements we add an indirection(*) operator preceded to ptr then the code will look like this,
#include<stdio.h> int main() { int arr[5] = { 1, 2, 3, 4, 5 },i; int *ptr = arr; for(i=0;i<5;i++){ printf("%d\n", *(ptr));//adding the pointer be //increment ptr++ to move on next array element ptr++; } return 0; }
Output:
1
2
3
4
5
Now it’s time to print array element address with value then the code will be,
#include<stdio.h> int main() { int arr[5] = { 1, 2, 3, 4, 5 },i; int *ptr = arr; for(i=0;i<5;i++){ printf("%p=>%d\n",ptr, *(ptr));//adding the pointer be //increment ptr++ to move on next array element ptr++; } return 0; }
Output:
0x7ffcaef1f190=>1
0x7ffcaef1f194=>2
0x7ffcaef1f198=>3
0x7ffcaef1f19c=>4
0x7ffcaef1f1a0=>5
Now another important concept replacing the printf(“%d”,*ptr) statement of the above example with below mentioned statements. Let’s see what will be the result.
printf("%d",a[i]); //prints the array, by incrementing index
printf("%d",i[a]); //prints the array, by incrementing index
printf("%u",(a+i)); //this will print address of all the array //elements
printf("%d",*(a+i)); //will print value of single array elelment //accordingly index i=0,1,2,3....
printf("%d",*a); //will print the value of a[0] only
a++; //compile-time error, we can't change the base address of an //array
Keep remembering the above formats in pointer with the 1-Dimensional array. And also see
*(a+i)
Same as,
a[i]
If we go more deeply then,
int *ptr;
int arr[5];
ptr = &arr[0];
The addresses for the rest of the array elements are given by &arr[1]
, &arr[2]
, &arr[3]
, and &arr[4]
.
Suppose we need to point to the fourth element means arr[4] of the array using the same pointer ptr.
Here, if ptr points to the first element i.e. a[0] in the above example then ptr + 3
will point to the fourth element. For example,
int *ptr;
int arr[5];
ptr = arr;
ptr + 1 is equivalent to &arr[1];
ptr + 2 is equivalent to &arr[2];
ptr + 3 is equivalent to &arr[3];
ptr + 4 is equivalent to &arr[4];
Similarly, we can access the elements using the single pointer. For example,
// use dereference operator
*ptr == arr[0];
*(ptr + 1) is equivalent to arr[1];
*(ptr + 2) is equivalent to arr[2];
*(ptr + 3) is equivalent to arr[3];
*(ptr + 4) is equivalent to arr[4];
Suppose if we have initialized ptr = &arr[2];
then
ptr - 2 is equivalent to &arr[0];
ptr - 1 is equivalent to &arr[1];
ptr + 1 is equivalent to &arr[3];
ptr + 2 is equivalent to &arr[4];

Note: The address between ptr and ptr + 1 differs by 4 bytes. It is because ptr is a pointer to int
data. And, the size of int is 4 bytes in a 64-bit operating system.
Similarly, if pointer ptr is pointing to char
type data, then the address between ptr and ptr + 1 is 1 byte. It is because the size of a character is 1 byte.
Now see an example:
// C++ Program to display address of each element of an array #include <iostream> using namespace std; int main() { float arr[3]; // declare pointer variable float *ptr; cout << "Displaying address using arrays: " << endl; // use for loop to print addresses of all array elements for (int i = 0; i < 3; ++i) { cout << "&arr[" << i << "] = " << &arr[i] << endl; } // ptr = &arr[0] ptr = arr; cout<<"\nDisplaying address using pointers: "<< endl; // use for loop to print addresses of all array elements // using pointer notation for (int i = 0; i < 3; ++i) { cout << "ptr + " << i << " = "<< ptr + i << endl; } return 0; }
Output:
Displaying address using arrays:
&arr[0] = 0x61fef0
&arr[1] = 0x61fef4
&arr[2] = 0x61fef8
Displaying address using pointers:
ptr + 0 = 0x61fef0
ptr + 1 = 0x61fef4
ptr + 2 = 0x61fef8
In the above program, we first simply printed the addresses of the array elements without using the pointer variable ptr.
Then, we used the pointer ptr to point to the address of a[0], ptr + 1
to point to the address of a[1], and so on.
Now we input and print the array using a pointer. Let’s see an example:
// C++ Program to insert and display data entered by using pointer notation. #include <iostream> using namespace std; int main() { float arr[5]; // Insert data using pointer notation cout << "Enter 5 numbers: "; for (int i = 0; i < 5; ++i) { // store input number in arr[i] cin >> *(arr + i) ; } // Display data using pointer notation cout << "Displaying data: " << endl; for (int i = 0; i < 5; ++i) { // display value of arr[i] cout << *(arr + i) << endl ; } return 0; }
Output:
Enter 5 numbers: 2.5
1.5
6.5
5
2
Displaying data:
2.5
3.5
4.5
5.0
2.0
Here, in the above program we take input cin>>*(arr+i) and it is the same as array arr[i]. Similarly, while displaying the array elements using pointer court<<*(arr+i) is the same as arr[i].