Avatar ·

How to convert std::vector to a plain array (C-style array) in C++?

Hello everyone! I'm writing a C++ program and ran into a problem. I have a std::vector that I'm filling with data, but one of the libraries I'm using expects a plain C-array (int*) as input. How can I get a raw pointer to the vector's internal data to pass it to the function?

\n\n

Here's an example of my code:

\n\n
#include <iostream>\n#include <vector>\n\nvoid process_array(int* arr, size_t size) {\n    for (size_t i = 0; i < size; ++i) {\n        std::cout << arr[i] << " ";\n    }\n}\n\nint main() {\n    std::vector<int> vec = {1, 2, 3, 4, 5};\n    \n    // How to get int* from vec?\n    // process_array(vec, vec.size()); // Error!\n    \n    return 0;\n}
\n\n

I've heard there's a method <