To access array elements in C++, you can use indexing with square brackets.
In this example,
numbers
with integer values.2
using indexing numbers[1]
.#include <iostream>
using namespace std;
int main() {
int numbers[] = {1, 2, 3, 4, 5};
cout << numbers[1] << endl;
return 0;
}
2
In this example,
fruits
with string values.#include <iostream>
using namespace std;
int main() {
string fruits[] = {"Apple", "Banana", "Orange"};
for (int i = 0; i < 3; i++) {
cout << fruits[i] << endl;
}
return 0;
}
Apple Banana Orange
In this tutorial, we learned How to Access Array Elements in C++ language with well detailed examples.