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
Different Ways to Convert Vector to Array in C++ STL In C++, vectors are dynamic arrays that can resize according to the number of elements. In this article, we will learn how to convert a vector to a C-style array in C++.The easiest way to convert a vector to array is by creating a new array and copy all the elements of vector into it using copy() fu
3 min read
How to copy elements of an Array in a Vector in C++ An Array is a collection of items stored at contiguous memory locations. The idea is to store multiple items of the same type together. Vectors are the same as dynamic arrays with the ability to resize themselves automatically when an element is inserted or deleted, with their storage being handled
6 min read
array data() in C++ STL with Examples The array::data() is a built-in function in C++ STL which returns an pointer pointing to the first element in the array object. Syntax: array_name.data() Parameters: The function does not accept any parameters. Return Value: The function returns an pointer. Below programs illustrate the above functi
2 min read
Pointer to an Array in C++ Pointers in C++ are variables that store the address of another variable while arrays are the data structure that stores the data in contiguous memory locations. In C++, we can manipulate arrays by using pointers to them. These kinds of pointers that point to the arrays are called array pointers or
6 min read
Pointer to an Array in C++ Pointers in C++ are variables that store the address of another variable while arrays are the data structure that stores the data in contiguous memory locations. In C++, we can manipulate arrays by using pointers to them. These kinds of pointers that point to the arrays are called array pointers or
6 min read
One Dimensional Arrays in C++ One-dimensional arrays are like a row of boxes where you can store things where each box can hold one item, such as a number or a word. For example, in an array of numbers, the first box might hold 5, the second 10, and so on. You can easily find or change what's in each box by referring to its posi
6 min read