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:
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:Code: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; }
it doesn't work as I expect - what am I missing here?Code:*data = (float **) calloc(nx, sizeof(float *)); for (i=0; i<nx; i++) *data[i] = (float *) calloc(ny, sizeof(float));
thanks for any help.
-Tim




Reply With Quote