9.3 Arrays & Pointers

The Relationship

Programs 9-5 to 9-8

Created for CCS120S

The Core Concept

Array names can be used as constant pointers!

An array name, without brackets and subscript, represents the starting address of the array.

This means: arrayName == &arrayName[0]

Three Uses of * in C++

  1. Multiplication: distance = speed * time;
  2. Pointer Definition: int *ptr = nullptr;
  3. Indirection Operator: *ptr = 100;

Dereferencing an Array Name


#include <iostream>
using namespace std;

int main()
{


   return 0;
}
                

Dereferencing an Array Name


#include <iostream>
using namespace std;

int main()
{
   short numbers[] = {10, 20, 30, 40, 50};

   return 0;
}
                

Dereferencing an Array Name


#include <iostream>
using namespace std;

int main()
{
   short numbers[] = {10, 20, 30, 40, 50};

   cout << "The first element of the array is ";
   cout << *numbers << endl;
   return 0;
}
                

Pointer Arithmetic

When you add a value to a pointer, C++ adds:

value × sizeof(dataType)

For short (2 bytes):

  • *(numbers + 1) → address + 2 bytes
  • *(numbers + 2) → address + 4 bytes

The Golden Rule

array[index]*(array + index)

The compiler converts subscripts to pointer arithmetic internally!

Memory Layout

Array elements are stored in contiguous memory locations:

10 20 30 40 50
arr[0] arr[1] arr[2] arr[3] arr[4]
0x1000 0x1002 0x1004 0x1006 0x1008
arr (base address = 0x1000)

*(arr + 2) = go to 0x1000 + (2 × 2 bytes) = 0x1004 → value: 30

Subscript ↔ Pointer Notation

Subscript Pointer Notation
arr[0] *arr
arr[1] *(arr + 1)
arr[2] *(arr + 2)
arr[3] *(arr + 3)
arr[4] *(arr + 4)

Both notations access the same memory location!

⚠️ Operator Precedence

NOTE: The * operator has higher precedence than +

Expression Meaning Result
*arr + 1 Get value at arr[0], then add 1 11 (if arr[0]=10)
*(arr + 1) Move to arr[1], then get value 20 (value at arr[1])

Using Pointer Notation


#include <iostream>
using namespace std;

int main()
{
   const int SIZE = 5;
   int numbers[SIZE];
   int count;


   return 0;
}
                

Using Pointer Notation


#include <iostream>
using namespace std;

int main()
{
   const int SIZE = 5;
   int numbers[SIZE];
   int count;

   // Use pointer notation instead of subscripts
   cout << "Enter " << SIZE << " numbers: ";
   for (count = 0; count < SIZE; count++)
      cin >> *(numbers + count);

   return 0;
}
                

Using Pointer Notation


#include <iostream>
using namespace std;

int main()
{
   const int SIZE = 5;
   int numbers[SIZE];
   int count;

   // Use pointer notation instead of subscripts
   cout << "Enter " << SIZE << " numbers: ";
   for (count = 0; count < SIZE; count++)
      cin >> *(numbers + count);

   // Display using pointer notation
   cout << "Here are the numbers you entered:\n";
   for (count = 0; count < SIZE; count++)
      cout << *(numbers + count) << " ";
   cout << endl;
   return 0;
}
                

⚠️ Bounds Checking Warning

WARNING! C++ performs NO bounds checking with arrays!

When stepping through an array with a pointer, you could accidentally access memory outside the array.

Valid Array (OK to access)
0
1
2
3
4
Out of Bounds (DANGER!)
5
6
Garbage data / Crash

🎯 Checkpoint

Q 9.7: Rewrite this loop using pointer notation:

for (int x = 0; x < 100; x++)
    cout << arr[x] << endl;
Answer:
for (int x = 0; x < 100; x++)
    cout << *(arr + x) << endl;

The Array-Pointer Duality

This program demonstrates:

  • Subscript notation with a pointer: ptr[i]
  • Pointer notation with an array: *(arr + i)

Both work because arrays and pointers share the same underlying mechanism!

Setting Up the Demo


#include <iostream>
using namespace std;

int main()
{
   const int NUM_COINS = 5;
   double coins[NUM_COINS] = {0.05, 0.1, 0.25, 0.5, 1.0};
   double *doublePtr;
   int count;

   return 0;
}
                

Connecting Pointer to Array


#include <iostream>
using namespace std;

int main()
{
   const int NUM_COINS = 5;
   double coins[NUM_COINS] = {0.05, 0.1, 0.25, 0.5, 1.0};
   double *doublePtr;
   int count;

   // Assign array address to pointer
   doublePtr = coins;

   return 0;
}
                

Subscript with Pointer


#include <iostream>
using namespace std;

int main()
{
   const int NUM_COINS = 5;
   double coins[NUM_COINS] = {0.05, 0.1, 0.25, 0.5, 1.0};
   double *doublePtr;
   int count;

   doublePtr = coins;

   // Use subscripts with the pointer!
   cout << "Here are the values in the coins array:\n";
   for (count = 0; count < NUM_COINS; count++)
      cout << doublePtr[count] << " ";

   return 0;
}
                

Pointer Notation with Array


#include <iostream>
using namespace std;

int main()
{
   const int NUM_COINS = 5;
   double coins[NUM_COINS] = {0.05, 0.1, 0.25, 0.5, 1.0};
   double *doublePtr;
   int count;

   doublePtr = coins;

   cout << "Here are the values in the coins array:\n";
   for (count = 0; count < NUM_COINS; count++)
      cout << doublePtr[count] << " ";

   // Use pointer notation with the array name!
   cout << "\nAnd here they are again:\n";
   for (count = 0; count < NUM_COINS; count++)
      cout << *(coins + count) << " ";
   cout << endl;
   return 0;
}
                

Getting Element Addresses

You can get the address of any array element using &:

&arrayName[index]

Using Element Addresses


#include <iostream>
using namespace std;

int main()
{
   const int NUM_COINS = 5;
   double coins[NUM_COINS] = {0.05, 0.1, 0.25, 0.5, 1.0};
   double *doublePtr = nullptr;
   int count;

   cout << "Here are the values in the coins array:\n";
   for (count = 0; count < NUM_COINS; count++)
   {
      // Get the address of an array element
      doublePtr = &coins[count];
      // Display the contents
      cout << *doublePtr << " ";
   }
   cout << endl;
   return 0;
}
                

Array Names are Constant Pointers

Legal:

dptr = readings;  // Pointer can be reassigned
dptr = totals;

Illegal:

readings = totals;  // ERROR! Cannot change array name
totals = dptr;      // ERROR!

Summary (الخلاصة)

  • Array name = address of first element (constant pointer)
  • array[i]*(array + i)
  • Subscript notation works with pointers: ptr[i]
  • Pointer notation works with arrays: *(arr + i)
  • Array names cannot be reassigned (constant)
Output