Different ways of accessing array elements in C++ - GeeksforGeeks

27 May, 2021
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++
// C++ program for the above concepts #include <iostream>using namespace std; // Driver Codeint 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;}
Value of a: 5
Address of a: 0x7ffe25768fa4
 
The symbol table for the program above would be like this:
 
Address 
 a
0x7ffe58e7cc4
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. 
Example: 
int a[10]; 
gets stored like:
*(a) - which points to the first element.
*(a+1) - which points to second element.
Similarly we can have the last element pointed by *(a+(n-1)) we have (n-1) as arrays in C++ have zero based indexing
Using this concept let us discuss the first method of accessing the arrays- 
Using the concept of pointers
 Below is the C++ program to implement the above concept: 
C++
// C++ program to demonstrate the// above approach#include <iostream>using namespace std; // Driver Codeint 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;}
Values : 1 2 3 4 5 6 7 8 9 10
 
Fun Method:
As observed, an array can be used as *(arr). Therefore, it can be said that: 
As known, *(p + 1) is exactly the same as *(1 + p)Therefore, *(arr + i) in above code can also be written as *(i + arr)and basically *(arr + i) means a[i] implying, *(i + arr) can also be written as i[a]
Below is the C++ program to implement the above concept: 
C++
// C++ program to demonstrate the// above approach#include <iostream>using namespace std; // Driver Codeint 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;}
Values: 1 2 3 4 5 6 7 8 9 10
 
shapnesht
CPP-Basics
Arrays
C++
C++ Programs
Arrays
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Stack Data Structure (Introduction and Program)
Multidimensional Arrays in Java
Queue | Set 1 (Introduction and Array Implementation)
Linear Search
Python | Using 2D arrays/lists the right way
Vector in C++ STL
Initialize a vector in C++ (6 different ways)
Map in C++ Standard Template Library (STL)
std::sort() in C++ STL
Inheritance in C++