C++ Pointer to an Array

In this Chapter we will learn and understand the concept of pointer to an array. It is important for you to know these 2 concepts before continuing with this tutorial:

  1. Arrays in C++
  2. Pointers in C++

Assuming you know both these concepts, lets get started. We know a pointer variable stores the address of another variable and can be used to access that variables data. Similarly a pointer can also point to an array. That is, a pointer can also be used to access all the values of an array.

An array is a collection of similar type(same datatype) elements which are stored in a contiguous memory location(one besides other). This property is suitable to for a pointer as it can be used to iterate through these contiguous memory locations. When we create an array, the array name basically points to the first address block of that array i.e. the base address. Thus the array name itself can be considered as an array.

Consider this example:

int Arr[] = {55,433,33,118};

Here we create an integer array named Arr. As shown in the diagram below, the base address (assumed to be 1000) is stored in the array name Arr. Since this is an integer array, every integer element takes up 4 bytes. Thus the second element will have an address of 1004 as shown in the diagram since they are stored in a contiguous manner.

 

array in c++

Now lets create a pointer variable of integer type and assign the address of the array Arr to this pointer.

int *p;
p = Arr;// or p = &arr[0];

 

Pointer to array in c++

As you can see in this diagram pointer p now has the base address of the array Arr and points to the first element of the array. Now we can access every element of array arr using p++ to move from one element to another.
NOTE : You cannot decrement a pointer once incremented. p– won’t work.

You may think, p ++ will give the address of next byte(that is 1001) but that is not true.
This is because pointer p is a pointer to an int and size of int is fixed for a operating system (size of int is 4 byte of 64-bit operating system). Hence, the address between p and p++ differs by 4 bytes.
If pointer p was pointer to char then, the address between p and p++ would have differed by 1 byte since size of a character is 1 byte.

Example 1 of Pointer to Array in C++

Run Online

#include <iostream>
using namespace std;
int main()
{
float arr[5];
float *ptr;
cout << "Displaying address using arrays: " << endl;
for (int i = 0; i < 5; ++i)
{
cout << "&arr[" << i << "] = " << &arr[i] << endl;
}
// ptr = &arr[0]
ptr = arr;
cout<<"\nDisplaying address using pointers: "<< endl;
for (int i = 0; i < 5; ++i)
{
cout << "ptr + " << i << " = "<< ptr + i << endl;
}

return 0;
}
Output

Displaying address using arrays:
&arr[0] = 0x7fff5fbff880
&arr[1] = 0x7fff5fbff884
&arr[2] = 0x7fff5fbff888
&arr[3] = 0x7fff5fbff88c
&arr[4] = 0x7fff5fbff890

Displaying address using pointers:
ptr + 0 = 0x7fff5fbff880
ptr + 1 = 0x7fff5fbff884
ptr + 2 = 0x7fff5fbff888
ptr + 3 = 0x7fff5fbff88c
ptr + 4 = 0x7fff5fbff890

Example 2 of Pointer to Array in C++

Run Online

#include <iostream>
using namespace std;

int main() {
float arr[5];

// Inserting data using pointer 
cout<<"Enter 5 numbers: ";
for (int i = 0; i < 5; ++i) {
cin >> *(arr + i) ;
}

// Displaying data using pointer 
cout<<"Displaying data: "<<endl;
for (int i = 0; i < 5; ++i) {
cout << *(arr + i) << endl ;
}

return 0;
}
Output

Enter 5 numbers: 50
35
5
5
20
Displaying data:
50
35
5
5
20

Watch it on YouTube

Leave a Reply

Your email address will not be published. Required fields are marked *