To access the first element of an array in C++, you can use indexing with square brackets and the index 0.
In this example,
numbers
with numeric values.numbers[0]
and store it in a variable named firstElement
.#include <iostream>
using namespace std;
int main() {
int numbers[] = {10, 20, 30, 40, 50};
int firstElement = numbers[0];
cout << "First element: " << firstElement << endl;
return 0;
}
First element: 10
In this example,
fruits
with string values.fruits[0]
and store it in a variable named firstFruit
.#include <iostream>
using namespace std;
int main() {
string fruits[] = {"Apple", "Banana", "Cherry", "Date"};
string firstFruit = fruits[0];
cout << "First fruit: " << firstFruit << endl;
return 0;
}
First fruit: Apple
In this tutorial, we learned How to get First Element in Array in C++ language with well detailed examples.