timfenn
December 29th, 2005, 01:47 PM
I'm still a bit new to C, and attempting to master pointers. I have a function that allocates a 2d pointer array, like so:
int main(int argc, char *argv[]){
float **data;
alloc_data(&data);
}
void alloc_data(float ***data){
int i, nx, ny;
float **tmp;
nx = 3;
ny = 3;
tmp = (float **) calloc(nx, sizeof(float *));
for (i=0; i<nx; i++)
tmp[i] = (float *) calloc(ny, sizeof(float));
*data = tmp;
}
works just fine, but how can I allocate data without having to alloc tmp and have data point to tmp? For example, if I do:
*data = (float **) calloc(nx, sizeof(float *));
for (i=0; i<nx; i++)
*data[i] = (float *) calloc(ny, sizeof(float));
it doesn't work as I expect - what am I missing here?
thanks for any help.
-Tim
int main(int argc, char *argv[]){
float **data;
alloc_data(&data);
}
void alloc_data(float ***data){
int i, nx, ny;
float **tmp;
nx = 3;
ny = 3;
tmp = (float **) calloc(nx, sizeof(float *));
for (i=0; i<nx; i++)
tmp[i] = (float *) calloc(ny, sizeof(float));
*data = tmp;
}
works just fine, but how can I allocate data without having to alloc tmp and have data point to tmp? For example, if I do:
*data = (float **) calloc(nx, sizeof(float *));
for (i=0; i<nx; i++)
*data[i] = (float *) calloc(ny, sizeof(float));
it doesn't work as I expect - what am I missing here?
thanks for any help.
-Tim