The array::data() is a built-in function in C++ STL which returns an pointer pointing to the first element in the array object.
Syntax:
array_name.data()
Parameters: The function does not accept any parameters.
Return Value: The function returns an pointer.
Below programs illustrate the above function:
Program 1:
// CPP program to demonstrate the // array::data() function #include <bits/stdc++.h> using namespace std; int main() { array<int, 5> arr = { 1, 2, 3, 4, 5 }; // prints the array elements cout << "The array elements are: "; for (auto it = arr.begin(); it != arr.end(); it++) cout << *it << " "; // Points to the first element auto it = arr.data(); cout << "\nThe first element is:" << *it; return 0; } |
The array elements are: 1 2 3 4 5 The first element is:1
Program 2:
// CPP program to demonstrate the // array::data() function #include <bits/stdc++.h> using namespace std; int main() { array<int, 5> arr = { 1, 2, 3, 4, 5 }; // prints the array elements cout << "The array elements are: "; for (auto it = arr.begin(); it != arr.end(); it++) cout << *it << " "; // Points to the first element auto it = arr.data(); // increment it++; cout << "\nThe second element is: " << *it; // increment it++; cout << "\nThe third element is: " << *it; return 0; } |
The array elements are: 1 2 3 4 5 The second element is: 2 The third element is: 3
Rated as one of the most sought after skills in the industry, own the basics of coding with our C++ STL Course and master the very concepts by intense problem-solving.
Recommended Posts:
- vector data() function in C++ STL
- Sorting an array according to another array using pair in STL
- array::front() and array::back() in C++ STL
- array::fill() and array::swap() in C++ STL
- array::begin() and array::end() in C++ STL
- array::crbegin() and array::crend() in C++ STL
- array::cbegin() and array::cend() in C++ STL
- array::rbegin() and array::rend() in C++ STL
- multiset lower_bound() in C++ STL with Examples
- multiset max_size() in C++ STL with Examples
- forward_list max_size() in C++ STL with Examples
- multiset size() in C++ STL with Examples
- unordered_set get_allocator() in C++ STL with Examples
- not1 and not2 function templates in C++ STL with Examples
- fill() function in C++ STL with examples
- fill_n() function in C++ STL with examples
- Vector of Vectors in C++ STL with Examples
- Map of Vectors in C++ STL with Examples
- Stack of Pair in C++ STL with Examples
- Queue of Pairs in C++ STL with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

