# Pointers Vs Array

September 15, 2022, Learn eTutorial
1196

There is a deep connection between the pointers and arrays. In many situations, pointers and arrays can actually be used interchangeably. For instance, a pointer that points to the start of an array can be used to access that array using either array-style indexing or pointer arithmetic.

Consider the following C++ program as an example:

``````#include <iostream>

using namespace std;
const int MAX = 5;

int main () {
int  var[MAX] = {20, 200, 400, 800, 1000};
int  *ptr;

// let us have array address in pointer.
ptr = var;

for (int i = 0; i < MAX; i++) {
cout << "Address of var will be [" << i << "] = ";
cout << ptr << endl;

cout << "Value of var will be[" << i << "] = ";
cout << *ptr << endl;

// point to the next location
ptr++;
}

return 0;
}```
```

Output:

```Address of var will be [0] = 0x7ffd063d4750
Value of var will be[0] = 20
Address of var will be [1] = 0x7ffd063d4754
Value of var will be[1] = 200
Address of var will be [2] = 0x7ffd063d4758
Value of var will be[2] = 400
Address of var will be [3] = 0x7ffd063d475c
Value of var will be[3] = 800
Address of var will be [4] = 0x7ffd063d4760
Value of var will be[4] = 1000
```

Pointers and arrays are not entirely interchangeable, though.

Through an example let us understand this more clearly

``````
#include <iostream>

using namespace std;
const int MAX = 3;

int main () {
int  var[MAX] = {10, 100, 200};

for (int i = 0; i < MAX; i++) {
*var = i;    //  correct syntax
var++;       // incorrect syntax
}

return 0;
}
```
```

While using the pointer operator * on var is permissible, changing the value of var is prohibited. This is because var cannot be used as an l-value because it is a constant that will always point to the starting of an array.

## Array of pointers

With a simple example let us understand the concept of an array of pointers. For that let us write a C++ program that mainly uses a set of four integers as an array.

``````
#include <iostream>

using namespace std;
const int MAX = 4;

int main () {
int  var[MAX] = {20, 200, 400, 800};

for (int i = 0; i < MAX; i++) {

cout << "Value of var[" << i << "] = ";
cout << var[i] << endl;
}

return 0;
}``````
```
```

Output:

```Value of var[0] = 20
Value of var[1] = 200
Value of var[2] = 400
Value of var[3] = 800
```

There may be times when we need to keep an array that can hold pointers to every possible data type, including int, char, and others.

The declaration of an array of pointers to an integer is provided below.

`int *ptr[MAX];`

According to this, ptr is a set of MAX integer pointers. As a result, a pointer to an int value is now stored in each element of ptr. The example below uses four integers, which are stored in the following array of pointers:

``````
#include <iostream>

using namespace std;
const int MAX = 4;

int main () {
int  var[MAX] = {100, 1000, 2000, 3000};
int *ptr[MAX];

for (int i = 0; i < MAX; i++) {
ptr[i] = &var[i]; // assign the address of an integer.
}

for (int i = 0; i < MAX; i++) {
cout << "Value of var[" << i << "] = ";
cout << *ptr[i] << endl;
}

return 0;
}```
```

Output:

```Value of var[0] = 100
Value of var[1] = 1000
Value of var[2] = 2000
Value of var[3] = 3000
```

You may also use an array of pointers in order to hold a list of strings as seen below.

``````
#include <iostream>

using namespace std;
const int MAX = 5;

int main () {
const char *names[MAX] = { "Pathu Khan", "Ali Kapoor", "Zera Vishal", "Kara Johar Ali Bose" };

for (int i = 0; i < MAX; i++) {
cout << "Value of names will be[" << i << "] = ";
cout << (names + i) << endl;
}

return 0;
}```
```

Output:

```Value of names will be[0] = 0x7fffe86d2910
Value of names will be[1] = 0x7fffe86d2918
Value of names will be[2] = 0x7fffe86d2920
Value of names will be[3] = 0x7fffe86d2928
Value of names will be[4] = 0x7fffe86d2930
```
VIEW ALL
VIEW ALL