Pass vector of char vectors to char**

376 views Asked by At

Is it possible to pass a std::vector<std::vector<char>> to a function void doSomething(char** arr) e.g. to store a bunch of paths

similar as with

std::vector<char> vec to void func(char *str)

vec.assign(64, ' ');

Function call: func(vec.data());

2

There are 2 answers

0
galinette On BEST ANSWER

Not directly, as the vector of vectors does not store an array to its children data adresses. So you have to build this array

std::vector<char*> temp(vec.size);
for(unsigned int i=0;i<vec.size();i++) {
    temp[i] = vec[i].data();
}

doSomething(temp.data());
0
leon22 On

Full example:

std::vector<std::vector<char>> vec(16, std::vector<char>(255));
std::vector<char*> temp(vec.size());
for(unsigned int i=0;i<vec.size();i++) 
{
    temp[i] = vec[i].data();
}

doSomething(temp.data());

Thx galinette!