C++ Accessing and Modifying Array Elements

In C++, you can access and modify individual elements of an array using square brackets [] and the index of the element you want to access or modify.

Here's an example of accessing an element in an array:

int myArray[5] = { 1, 2, 3, 4, 5 };
int thirdElement = myArray[2]; // thirdElement is now 3
Sourc‮e‬:www.theitroad.com

In this example, myArray[2] retrieves the third element of the array (remember, array indices start at 0), which is the value 3. The value of myArray[2] is then assigned to the variable thirdElement.

You can also modify elements of an array using the same square bracket syntax. Here's an example of modifying an element in an array:

int myArray[5] = { 1, 2, 3, 4, 5 };
myArray[2] = 10; // myArray is now { 1, 2, 10, 4, 5 }

In this example, myArray[2] is assigned the value 10, which replaces the original value of 3 at that index.

Note that you must ensure that the index you are accessing or modifying is within the bounds of the array. If you attempt to access or modify an element outside the bounds of the array, you may get unexpected behavior or a runtime error.