Introduction to C++ Operator[]
The array index operator or subscript denoted by [] is used to retrieve and manipulate the elements of the array and is used with the arrays and it is basically an array operator or a binary operator whose representation can be divided into two parts like primary expression or postfix expression and an expression among which primary expression or postfix expression is a pointer value like identifiers or array and integral value is the second expression in which enumerated values are also included. The syntax of C++ operator[] is as follows:
Syntax:
postfix-expression[expression];
An example to declare C++ operator [] is as follows:
Shobha[28];
In the above example, Shobha represents the array and upon executing the above statement, the value that is held by Shobha at a position having the index 28 of Shobha array is printed. The pointer is the subscript operator which has a primary expression and it must be an integral value but an important point be noted is that in an C++ operator[], one of the two expressions must be a pointer value and it does not matter if the second value is an integral value or no.
Working of C++ Operator[] Function
An expression consisting of a postfix expression followed by [] (brackets) consisting of an expression specifying the position of an element in the array is called array subscripting operator in C++. The expression inside the brackets is called subscript. The array’s first element has the subscript zero. There are no array bounds for built in types of array. Consider the example a[b] which can be interpreted as *((a) + (b)). This is also equal to b[a] because addition is associative. Among the expressions a and b in our example a[b], one the expressions must be a pointer value to support the type T and the other expression must be an integral value or enumeration type. Lvalue is the result of an array subscript. Consider the following example demonstrating the array subscript operator in C++:
Code:
//including the standard libraries
#include <stdio.h>
//calling the main method and it returns an integer value
int main(void) {
//Declaring an array
int x[3] = { 50, 60, 70 };
//printing the array value having the index 0
printf("x[0] = %d\n", x[0]);
//printing the array value having the index 1
printf("x[1] = %d\n", 1[x]);
//printing the array value having the index 2
printf("x[2] = %d\n", *(2 + x));
return 0;
}
Output:
In the above example, the variable x represents an array and 3 in the brackets followed by the array name x represents the number of positions held by the array x. In this example, the array x holds three integral values. The three print statements print the values having the indexes at 0, 1 and 2 in the array x.
Examples to Implement C++ Operator[]
Below are the examples of C++ Operator[]:
Example #1
C++ program to implement and demonstrate the use of an array index operator [].
Code:
//including the standard libraries
#include <iostream>
//using a namespace
using namespace std;
//calling the main method which returns an integer value
int main()
{
//defining a character array consisting of only characters and there is no limit on the number of positions on the array
char check[] = "Shobha";
//Printing the third character in the array starting from zeroth position, two different print statements are used but both the statements print the same result
cout << check[3] << endl;
cout << 3 [check] << endl;
return 0;
}
Output:
Explanation:
In the above example, the standard library in C++ iostream is included. A namespace std is defined and used. The main method is defined to return an integer value. A character array consisting of only characters is defined and there is no limit on the number of characters that can be stored in the array. The program then makes use of array subscript operator in C++ to print the third element in the array starting from the zeroth element. Two different print statements are used here which displays the same result because of the exclusive property of the array subscript operator in C++. Both the statements check[3] and 3[check] are read by the compiler in a similar fashion and hence there is no difference between the two statements check[3] and 3[check] and they print the same result.
Negative Subscripts
The first array element is always stored at the index zero. The C++ array ranges from array[0] to array[whole size of array – 1]. But C++ supports both positive array subscript operator and negative array subscript operator. The negative array subscript operator can be used to declare negative indexes. There are boundaries within which the negative subscripts must be ranging and if they fall out of the range, the results are beyond prediction.
Example #2
C++ program to implement and demonstrate positive and negative array subscript operators.
Code:
//including the standard libraries
#include <iostream>
using namespace std;
//calling the main method which returns an integer value
int main()
{
//declaring an integer array which can consist of 1025 elements
int check[1025];
for (int x = 0, y = 0; x < 1025; x++) {
check[x] = y++;
}
//printing the element positioned at 513 of array check
cout << check[513] << endl;
//printing the element positioned at 256 of array check
cout << 256 [check] << endl;
// declaring a pointer to the center of the array
int* midcheck = &check[513];
//printing the element positioned at -257 of array midcheck
cout << midcheck[-257] << endl;
//printing the element positioned at -257 of array check whose results can be unpredictable
cout << check[-257] << endl;
}
Output:
Explanation:
In the above example, the standard library in C++ iostream is included. A namespace std is defined and used. The main method is defined to return an integer value. An integer array consisting of 1025 elements is defined. The program then makes use of array subscript operator in C++ to print the 513th element in the array starting from the zeroth element. The program then makes use of array subscript operator in C++ to print the 256th element in the array starting from the zeroth element. The program then makes use of array subscript operator in C++ to print the -257th element in the array starting from the zeroth element by declaring a new array called midcheck consisting of 513 elements. The program then makes use of array subscript operator in C++ to print the -257th element in the array starting from the zeroth element which results in an unpredictable output.
Conclusion – C++ Operator[]
In this article, we have learnt about array subscript operator[] in C++ through definition, syntax, working of array subscript operator[], examples to demonstrate the implementation positive and negative
Recommended Articles
This is a guide to C++ Operator[]. Here we discuss the working of Operator[] Function in C++ and its Examples along with its Code Implementation. You can also go through our other suggested articles to learn more –
4 Online Courses | 5 Hands-on Projects | 37+ Hours | Verifiable Certificate of Completion
4.5
View Course
Related Courses