Array Notations in C++
In C++, an array is collection of elements that have the same type and are referred to by a common name. In arrays, elements are accessed using array notation. We can access and assign new value using array indexing. The name of an array acts as a constant pointer to the first element. For example, arr can be used as a pointer to arr[0].
Syntax of Array Notations
arrName[index];
Here,
- arrayName is the name of the array.
- index is the position in an array that you want to access or manipulate.
Example
The below example demonstrates the use of array notation in C++.
// C++ program to use array notation
#include <iostream>
using namespace std;
int main()
{
// define an array
int arr[5] = { 10, 22, 30, 44, 50 };
// access 0th index element using array notation and
// print it
cout << "First element: " << arr[0] << endl;
return 0;
}
Output
First element: 10
To learn more about arrays in C++ refer: C++ Arrays
Difference Between Pointers and Array Notations in C++
In C++, pointers and array notations are two ways using which we work with arrays and memory for accessing the data. They have distinct behaviours and are used in different contexts. In this article, we will learn the key differences between pointers and array notations in C++.