Is there a way to pass ‘std::vector 2d’ as a pointer to ‘2d c array’ to a function.
I know you can pass std::vector 1d as a pointer to c array to some function.
for example,
function:
void foo(char* str); //requires the size of str to be 100 chars
std::vector<char> str_;
str_.resize(100);
foo(&str_[0]); //works
I’m wondering if it is possible for 2d vectors too like for
function
void foo(char** arr_2d);
and vector
std::vector<std::vector<char>> vector_2d;
I tried the following code but im getting some error related to heap corruption.
std::vector<std::vector<unsigned char>> vector_2d;
//assuming function expects the size of the vector to be 10x10
vector_2d.resize(10);
for(int i=0;i<10;i++)
{
vector_2d[i].resize(10);
}
foo(&vector_2d[0]);//error here
Here’s what you can do: