December 22, 2024

Connection Between Arrays and Pointers in C++ – C++ Tutorial

In this tutorial, we explain a very important connection between arrays and pointers in C++. The YouTube tutorial accompanying this webpage tutorial is given below.

Before we explain the connection between arrays and pointers, we first need to explain:

  1. How variables are stored in the computer memory.
  2. How arrays are stored in the computer memory

How variables are stored in the computer memory

  • The classical computer memory is composed of numbered locations.
  • Every numbered location is called a byte. A byte consists of 8 bits, and every bit is either 0 or 1. An example of the byte is: “01011100”
  • Every byte has an address in the computer’s memory. The byte is accessed by using its address.

The figure below is a sketch of the computer memory with bytes and addressed. The bytes are rectangular slots. Next to every byte, we have its address that is used to access that particular byte.

  • A primitive variable (simple variable) is implemented as a segment of the memory space composed of the consecutive bytes.
  • Variables of the int type usually occupy 4 consecutive bytes (can also be 2 bytes).
  • This implies that a variable is described by: 1) an address in the memory that determines the location of the first byte; 2) type of the variable which determines how many consecutive bytes it needs to occupy.

These points are illustrated in the figure below.

Since both “a” and “b” are integers they occupy 4 bytes each in the memory space. These bytes are consecutive for every variable and they are represented by the shaded regions.

How array variables are stored in the memory

  • When an array is declared the computer reserves the number of memory locations (bytes) corresponding to the data type that the array stores and the total number of variables that need to be stored.
  • The locations of array members are consecutively placed in the memory. That is, they are placed next to one another in memory.
  • The computer only remembers the address of the first byte of  the first variable in the array (that is, the beginning of the array) and the total number of variables in the array. That is, it does not remember the memory location of every variable in the array or the memory location of their bytes.
  • When the computer needs the address of some other variable it will simply offset or calculate the address of the first variable. This offset is computed on the basis of the location of the variable in the array

This is illustrated in the figure below

To access or modify any variable in the array, the compiler needs to know only the address of the first byte of a[0] and the data type of the stored variable. Then, it will simply offset the address of the first byte of a[0] to access any other variable. For example, to access a[1], the address of a[0] has to be offset by 4 in the figure above.

Arrays and pointers

Now we are ready to explain the connection between arrays and pointers. Let us decleare an array a:

int a[3]

  • The array name “a” (array variable) is actually a pointer to the memory address of the first indexed variable in the array. That is, it is a pointer to (the first byte of) a[0].
  • We can define a new pointer variable like this:
    int* p
    and write
    p=a
    Now p points to a. That is, it points to a[0]. We can access and modify the members of a: a[0], a[1], a[2], by using  p. That is p[0], p[1], p[2], …, can be used instead of a[0], a[1], a[2], …, .
  • Once the pointer a is defined it cannot be changed! That is, a is not of the type int* it is of the const version of int*. For example, this assignment is not legal in C++:
    a= p1
    where p1 is a previously declared pointer that points to some other address.

To conclude, array names are actually pointers. Newly defined pointers can be used to point to the names of the variables of arrays, and we can use these new pointers and the operator [] to access the variables stored in the array. The code below explains this and gives an example on how to use pointers and arrays:

// Program that demonstrates that array names are pointers

// The array name is a constant pointer that points to the first indexed variable of the array

#include<iostream>
using namespace std;

int main()
{
// let us declare an array
int a[10];
// pointer to integer
int *p;
for (int index=0; index<10; index++)
{
    a[index]=index;
}
// now p points to the same memory location that a points to
p=a;
// print now the entries of the array a by using p
for (int index=0; index<10; index++)
{
    cout<<p[index]<<" ";
}
cout<<endl;
// let us now change a by changing p
// let us double the entries of p
for (int index=0; index<10; index++)
{
    p[index]=2*index;
}

// let us print the entries of a by printing the entries of p

// print now the entries of the array a
for (int index=0; index<10; index++)
{
    cout<<a[index]<<" ";
}
cout<<endl;



return 0;
}