Arrays are ordered, meaning each element has a numbered position known as its index. We access an array element by referring to its index number.
Arrays use zero-based indexing, so the first element in an array has an index of 0, the second element has an index of 1, etc.
We’re using bracket notation ([]) with the index after the name of the array to access the element:
string animals[] = {"cat", "dog", "parrot"};
cout << animals[0] << endl; // Output: "cat"
cout << animals[1] << endl; // Output: "dog"
cout << animals[2] << endl; // Output: "parrot"
Assignment
Follow the Coding Tutorial and let's practice with accessing elements!
Hint
Look at the examples above if you get stuck.
Free plan: 30 tutor messages. PRO is unlimited.
