I am working with C++ to construct an array, which is then passed into Python (similar to: Embed python / numpy in C++). I am new to C++ and I am confused with some of the details of the code. I'm hoping I can get an understanding of how this code works, because I need to change it. So my question is: what is this method of initializing an array?
const int SIZE{ 10 };
double(*c_arr)[SIZE]{ new double[SIZE][SIZE] };
For the record, I've been able to make this a rectangular array by calling:
const int numberRows = 5000;
const int numberColumns = 500;
double(*c_arr)[numberColumns]{ new double[numberRows][numberColumns] };
I fill the array:
// fill the array from a file
std::string line;
int row = 0;
int column = 0;
while (std::getline(dataFile, line)) {
std::stringstream lineStream(line);
std::string cell;
while (std::getline(lineStream, cell, '\t')) {
c_arr[row][column] = std::stod(cell);
column++;
}
row++;
column = 0;
if (row == numberRows) {
break;
}
}
I still don't understand what is meant by double(*c_arr)
. Whenever I try to initialize this array differently, I get errors. For example: double *c_arr[numberRows][numberColumns];
raises errors when I try to fill the array c_arr[row][column] = std::stod(cell);
. If I change the initialization to be: double c_arr[numberRows][numberColumns];
, then I get a segmentation fault when run. What I'd like to eventually achieve is a function that returns a pointer to an array; something like:
double *load_data(int rows, int columns) {
double(*c_arr)[columns]{ new double[rows][columns] };
//fill array here
return (c_arr)
}
When I construct such a function, I get an error at the second occurrence of the columns
variable: expression must have a constant value -- the value of parameter "columns" cannot be used as a constant
. I don't really how to do this, but I'm hoping that if I can understand the array initialization, I'll be able to properly construct the load_data
function.