Different ways of accessing array elements in C++
Last Updated :
13 Apr, 2023
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 Code
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;
}
OutputValue of a: 5
Address of a: 0x7ffefad3072c
The symbol table for the program above would be like this:
Variable Name | Address
| Value |
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.
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;
}
OutputThe number is=11
12
13
14
15
16
Addition of numbers= 81
average of numbers= 13.5
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 Code
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;
}
OutputValues : 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 Code
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;
}
OutputValues: 1 2 3 4 5 6 7 8 9 10
Similar Reads
C++ Programming Language C++ is a computer programming language developed by Bjarne Stroustrup as an extension of the C language. It is known for is fast speed, low level memory management and is often taught as first programming language. It provides:Hands-on application of different programming concepts.Similar syntax to
5 min read
Object Oriented Programming in C++ Object Oriented Programming - As the name suggests uses objects in programming. Object-oriented programming aims to implement real-world entities like inheritance, hiding, polymorphism, etc. in programming. The main aim of OOP is to bind together the data and the functions that operate on them so th
5 min read
30 OOPs Interview Questions and Answers [2025 Updated] Object-oriented programming, or OOPs, is a programming paradigm that implements the concept of objects in the program. It aims to provide an easier solution to real-world problems by implementing real-world entities such as inheritance, abstraction, polymorphism, etc. in programming. OOPs concept is
15 min read
Inheritance in C++ The capability of a class to derive properties and characteristics from another class is called Inheritance. Inheritance is one of the most important features of Object-Oriented Programming in C++. In this article, we will learn about inheritance in C++, its modes and types along with the informatio
10 min read
Vector in C++ STL C++ vector is a dynamic array that stores collection of elements same type in contiguous memory. It has the ability to resize itself automatically when an element is inserted or deleted.Create a VectorBefore creating a vector, we must know that a vector is defined as the std::vector class template i
7 min read
Templates in C++ C++ template is a powerful tool that allows you to write a generic code that can work with any data type. The idea is to simply pass the data type as a parameter so that we don't need to write the same code for different data types.For example, same sorting algorithm can work for different type, so
9 min read
C++ Interview Questions and Answers (2025) C++ - the must-known and all-time favourite programming language of coders. It is still relevant as it was in the mid-80s. As a general-purpose and object-oriented programming language is extensively employed mostly every time during coding. As a result, some job roles demand individuals be fluent i
15+ min read
Operator Overloading in C++ in C++, Operator overloading is a compile-time polymorphism. It is an idea of giving special meaning to an existing operator in C++ without changing its original meaning.In this article, we will further discuss about operator overloading in C++ with examples and see which operators we can or cannot
8 min read
C++ Standard Template Library (STL) The C++ Standard Template Library (STL) is a set of template classes and functions that provides the implementation of common data structures and algorithms such as lists, stacks, arrays, sorting, searching, etc. It also provides the iterators and functors which makes it easier to work with algorith
9 min read
C++ Classes and Objects In C++, classes and objects are the basic building block that leads to Object-Oriented programming in C++. We will learn about C++ classes, objects, look at how they work and how to implement them in our C++ program.C++ ClassesA class is a user-defined data type, which holds its own data members and
9 min read