I am trying to access values of a 2D array initially passed as an array of pointers in the function. Additionally I would like this function to return a 2D array with the values I have accessed and worked on.
I understand the concept of pointer, however I have trouble with the double pointer concept of 2D arrays. If I understand correctly, iterating over the first dimension of the array points to a position in the memory of the computer, which itself points to another position of the memory in the computer where the value is. This 2 pointing process represents the 2D.
Now, in my code, I don't manage to access the values of this second pointer and I don't understand why.
extern "C" double cfun(double **indatav, int rows, int cols)
{
double* *outdatav = new double*[rows];
for (int i=0; i < rows; i++){
outdatav[i] = new double*[cols];
}
for (int i=0; i < rows; i++){
for (int j=0; j < cols; j++){
outdatav[i][j] = indatav[i][j];
}
}
return outdatav;
}
The code above is called through ctypes in Python and therefore it has this extern... However the equivalent in C++ would just consist in removing this extern "C"
I have spent quite some time now on this problem and I don't think I will be able to solve it alone so any help would be highly appreciated.
*indata[i][j]is not valid.intdatav[i]IS a pointer and points to the start of row I. Just useoutdatav[i][j] = indatav[i][j];