Different ways of accessing array elements in C++

Last Updated : 16 Jan, 2026

In this article, two unique and different ways of accessing an element from an array rather than the conventional arr[i] expression are discussed below.

  • Using pointer *(arr+1)
  • Using a little manipulation i[arr] for using arrays and the reason behind that.

When a C++ program is compiled, a symbol table is generated simultaneously. It is formed to store the corresponding values of the address of all the variables used in the program.

Let us consider a C++ program and see what will the symbol table for the corresponding program:

C++
#include <iostream>
using namespace std;

int main(){
    // Assigning a value to a
    int a = 5;

    // Printing the value of a
    cout << "Value of a: " << a;

    // Printing the address of a
    cout << "\nAddress of a: " << &a;
    return 0;
}

Output
Value of a: 5
Address of a: 0x7ffefad3072c

The symbol table for the program above would be like this:

  • Variable Name: a
  • Address: 0x7ffe58e7cc4
  • Value: 5

So, from the symbol table, it can be seen that every variable is assigned an address. Therefore, when the array is initialized, it also gets some address. In the table, an array gets stored in form of a pointer pointing towards the first element. 

The below example is another simplest way of accessing array elements in C++.

C++
#include <iostream>
using namespace std;

int main()
{
    double num[] = { 11, 12, 13, 14, 15, 16 };
    double add = 0;
    double count = 0;
    double avg;
    cout << "The number is=";

    for (const double& n : num) {
        cout << n << " " <<endl;

        add += n;

        ++count;
    }
    cout << "Addition of numbers= " << add << endl;
    avg = add / count;
    cout << "average of numbers= " << avg << endl;

    return 0;
}

Output
The number is=11 
12 
13 
14 
15 
16 
Addition of numbers= 81
average of numbers= 13.5

Using the concept of Pointers

When we declare an array like int a[10];, the name a stores the base address of the array, which is the address of its first element. Using pointer notation, *(a) refers to the first element of the array, which is the same as a[0]. Similarly, *(a + 1) refers to the second element, a[1], *(a + 2) refers to the third element, and so on. In general, *(a + (n - 1)) refers to the last element of an array of size n. This is because arrays in C++ use zero-based indexing, so the index of the last element is always n - 1.

C++
#include <iostream>
using namespace std;

int main(){
    int arr[10];

    // Conventional method
    // for(int i = 0; i<10; i++)
    //{
    // arr[i] = i+1;
    //}

    // Pointer Method
    for (int i = 0; i < 10; i++) {
        *(arr + i) = i + 1;
    }

    cout << "Values : ";

    for (int i = 0; i < 10; i++) {
        cout << *(arr + i) << ' ';
    }
    return 0;
}

Output
Values : 1 2 3 4 5 6 7 8 9 10 

Fun Method

As observed, an array name can be used as a pointer to its first element, that is, arr can be treated as *(arr). Since pointer addition is commutative, *(p + 1) is the same as *(1 + p). For the same reason, *(arr + i) can also be written as *(i + arr). Because *(arr + i) represents arr[i], it follows that *(i + arr) is also equal to arr[i]. Hence, this expression can even be written as i[arr], which is completely valid in C++ due to the way array indexing is defined in terms of pointer arithmetic.

C++
#include <iostream>
using namespace std;

int main(){
    int arr[10];

    // Conventional method
    // for(int i = 0; i<10; i++)
    //{
    // arr[i] = i+1;
    //}

    // Method 2
    for (int i = 0; i < 10; i++) {
        i[arr] = i + 1;
    }

    cout << "Values: ";

    for (int i = 0; i < 10; i++) {
        cout << i[arr] << ' ';
    }
    return 0;
}

Output
Values: 1 2 3 4 5 6 7 8 9 10 
Comment