Hi all,
I'm writing a program where I am allocating a 2d array dynamically (ie using malloc), and then I want to pass this array to a function, but I'm not being successful...
Here are the relevant parts of the program:
Code:
int main(void)
{
double **data; // 2d array to hold input file values
const unsigned int ncols=29; // number of columns to read
unsigned int nrows=0; // number of rows in input file
/* allocate data array */
**data=malloc(nrows*sizeof(*data));
if (data==NULL) {
printf("Could not allocate data array of size %i\n",nrows*sizeof(*data));
return 1;
}
for (i=0; i<nrows; i++) {
data[i]=malloc(ncols*sizeof(*data[i]));
if (data[i]==NULL) {
printf("Could not allocate array data[%i] of size %i\n",i, ncols*sizeof(*data[i]));
return 1;
}
}
/* read data into data array */
read_data(nrows, ncols, data);
.
.
.
}
and the function header for read_data is:
Code:
void read_data(unsigned int rows, const unsigned int cols, double *data[][cols]);
gcc (-Wall -pedantic) says:
Code:
lsis.least.sq.c:81: error: incompatible type for argument 3 of `read_data'
I've tried combinations of *data[][ncols], **data, etc to use as my argument for read_data, but it doesn't work. I know that when passing multidimensional array, you need to define the last dimension, but I'm not sure why it doesn't work the ways I've been trying.
Any sugggestions?
Thanks
Spiros