I have to convert from std::array<std::array<int, 4>, 4> to int** because some legacy api expects the result in int**.
I tried to do it like this:
#include <array>
#include <iostream>
std::array<int *, 4> toIntStar(std::array<std::array<int, 4>, 4> &v) {
int n = static_cast<int>(v.size());
std::array<int *, 4> a;
for (int i = 0; i < n; ++i) {
a[i] = &v[i][0];
}
return a;
}
int **somefunction() {
std::array<std::array<int, 4>, 4> cpparray{
{{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16}}};
std::array<int *, 4> resultStar = toIntStar(cpparray);
int **result = &resultStar[0];
return result;
}
int main() {
auto result = somefunction();
std::cout << result[0][0] << '\n';
std::cout << result[0][1] << '\n'; // crashes here
//....
}
However it crashes in the line:
std::cout << result[0][1] << '\n'; // crashes here
Were is the mistake here?