
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
An Uncommon representation of array elements in C++ program
An array is a linear data structure that stores elements the same data type. To access a single data element of the array, there is a standard way that is commonly used.
Syntax
array_name[index];
Example
#include <iostream> using namespace std; int main( ){ int arr[2] = {32,65}; printf("First Element = %d\n",arr[0]); printf("Second Element = %d\n",arr[1]); return 0; }
Output
First Element = 32 Second Element = 65
Now, there is another method that can provide the same output as the above.
Syntax
index[array_name];
Example
#include <iostream> using namespace std; int main( ){ int arr[2] = {32,65}; printf("First Element = %d\n",0[arr]); printf("Second Element = %d\n",1[arr]); return 0; }
Output
First Element = 32 Second Element = 65
Let’s take under consideration both the cases −
arr[0] would be *(arr + 0) pointer that points to a value.
0[arr] would be *(0 + arr) pointer that points the same as the former one does.
Both the pointers point to the same memory address.
- Related Articles
- An Uncommon representation of array elements in C/C++
- Program to find uncommon elements in two arrays - JavaScript
- Golang Program to find the uncommon elements from two arrays
- Golang Program to Rotate Elements of an Array
- Swift Program to Count the elements of an Array
- Golang program to shuffle the elements of an array
- C program to reverse an array elements
- Print uncommon elements from two sorted arrays
- C program to sort an array of ten elements in an ascending order
- Java Program to fill elements in an int array
- C# Program to skip initial elements in an array
- C++ program to reverse an array elements (in place)
- C++ Program to Access Elements of an Array Using Pointer
- Python program to left rotate the elements of an array
- Python program to print the duplicate elements of an array

Advertisements