.. / , , char*[2]; new, .
:
char c[2][2] char. 4 char .
When you create an array of arrays ( char** c), you need to manually select an array of pointers to char, and then select (or assign) an array to chareach of these pointers.
So, to convert an array char c[2][2]to an array of arrays, you first need to create an array of pointers, and then assign that array the first element of each char array.
Something like that:
void func(char** c)
{
for(int x = 0; x < 2; ++x)
for(int y = 0; y < 2; ++y)
std::cout << c[x][y] << ' ';
std::cout << '\n';
}
int main(int, char* argv[])
{
char c[2][2]={
{'a','b'},
{'c','d'}
};
char** param = new char*[2];
param[0] = &c[0][0];
param[1] = &c[1][0];
func(param);
delete[] param;
}
If you have one C++11, you can use the smart pointer to prevent a memory leak if an exception is thrown or someone has forgotten delete.
int main(int, char* argv[])
{
char c[2][2]={
{'a','b'},
{'c','d'}
};
std::unique_ptr<char*[]> param(new char*[2]);
param[0] = &c[0][0];
param[1] = &c[1][0];
func(param.get());
}
Galik source
share