C++ Pointer and Arrays
In this tutorial, we will learn about the relation between arrays and pointers and how we use them with the help of examples.
Pointer and Arrays
- In C++, Pointers are variables that hold addresses of other variables. Not only can a pointer store the address of a single variable, it can also store the address of cells of an array.
- Array and Pointers are vary closely related to each other.
- The name of an array is considered as a pointer, i.e, the name of an array contains the address of an element.
- C++ considers the array name as the address of the first element.
Consider this example:
int *p;
int arr[5];
// store the address of the first
// element of arr in ptr
p = arr;
Here, p is a pointer variable while arr is an int
array. The code
p = arr;
stores the address of the first element of the array in variable p.
Notice that: we have used arr
instead of &arr[0]
. This is because both are the same.
So, the code below is the same as the code above.
int *p;
int arr[5];
p = &arr[0];
The addresses for the rest of the array elements are given by &arr[1]
, &arr[2]
,
&arr[3]
, and &arr[4]
.
Pointing to Every Element of an Array
Suppose we need to point to the fourth 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 arr[5];
p = arr;
p + 1 is equivalent to &arr[1];
p + 2 is equivalent to &arr[2];
p + 3 is equivalent to &arr[3];
p + 4 is equivalent to &arr[4];
Similarly, we can access the elements using the single pointer. For example,
// use dereference operator
*p == arr[0];
*(p + 1) is equivalent to arr[1];
*(p + 2) is equivalent to arr[2];
*(p + 3) is equivalent to arr[3];
*(p + 4) is equivalent to arr[4];
Note: The address between p and p + 1 differs by 4 bytes. It is
because p is a pointer to an int
data. And, the size of int is 4 bytes in a 64-bit
operating system.
Similarly, if pointer p is pointing to char
type data, then the address between
p and p + 1 is 1 byte. It is because the size of a character is 1 byte.
Example 1: C++ Traversing the Array using Ponters
#include <iostream>
using namespace std;
int main() {
int *ptr; // Integer pointer declaration
int arr[] = {1, 1, 2, 3, 4};
cout << "The Traversing of array: ";
ptr = arr;
for (int i = 0; i < 4; i++) {
cout << *ptr <<endl;
// ++ moves the pointer to next int position
ptr++;
}
return 0;
}
Output
The traversing of array is 1 2 3 4 5
Example 2: Program to display address of each element of an array
To print the content of a void pointer, we use the static_cast
operator. It converts the pointer from void*
type to the respective data type of the address the pointer is storing:
#include <iostream>
using namespace std;
int main(){
float arr[2];
// 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 < 2; ++i){
cout << "&arr[" << i << "] = " << &arr[i] << endl;
}
p = arr; // it can also be written as: p = &arr[0]
cout<<"\nDisplaying address using pointers: "<< endl;
// use for loop to print addresses of all array elements using pointer notation
for (int i = 0; i < 2; ++i){
cout << "p + " << i << " = "<< p + i << endl;
}
return 0;
}
Output
Displaying address using arrays: &arr[0] = 0x7ffe73135d70 &arr[1] = 0x7ffe73135d74 Displaying address using pointers: p + 0 = 0x7ffe73135d70 p + 1 = 0x7ffe73135d74
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 arr[0], p + 1
to point to the address of arr[1].
In most contexts, array names decay to pointers. In simple words, array names are converted to pointers. That's the reason why we can use pointers to access elements of arrays.
However, we should remember that pointers and arrays are not the same.
There are a few cases where array names don't decay to pointers.
To learn more, visit: When does array name doesn't decay into a pointer?
Example 3: Program to insert and display data entered by using pointer notation.
#include <iostream>
using namespace std;
int main() {
float arr[4];
// Insert data using pointer notation
cout << "Enter the 4 numbers: " << endl;
for (int i = 0; i < 4; ++i) {
cin >> *(arr + i); // store input number in arr[i]
}
// Display data using pointer notation
cout << "Displaying the data: " << endl;
for (int i = 0; i < 4; ++i) {
// display value of arr[i]
cout << *(arr + i) << endl ;
}
return 0;
}
Output
Enter the 4 numbers: 1 2.5 3.0 4 Displaying the data: 1 2.5 3.0 4
Here,
-
We first used the pointer notation to store the numbers entered by the user into the array arr.
cin >> *(arr + i) ;
This code is equivalent to the code below:
cin >> arr[i];
Notice that we haven't declared a separate pointer variable, but rather we are using the array name arr for the pointer notation.
As we already know, the array name arr points to the first element of the array. So, we can think of arr as acting like a pointer.
-
Similarly, we then used
for
loop to display the values of arr using pointer notation.cout << *(arr + i) << endl ;
This code is equivalent to
cout << arr[i] << endl ;
Next Tutorial
We hope that this tutorial helped you develop better understanding of the concept of Pointer & Arrays in C++.
Keep Learning : )
In the next tutorial, you'll learn about C++ Pointer & Functions
.