tags:

views:

92

answers:

2

Hi,

in a C program I have this variable:

float (*data)[3];
data = (float (*)[3])calloc(ntimes, sizeof(float[3]));

which later is initialized. Afterwards I want to pass all the info in "data" to a serie of ntimes variables called "data_i" that I define with:

typedef float (*el_i)[3];
el_i data_i[NTIMES];  // NTIMES is a constant with value "ntimes"
for (i = 0; i < ntimes; i++) data_i[i] = (float (*)[3])calloc(1, sizeof(float[3]));

and then I try to pass the info from "data" to all the "data_i"''s with

    for (i = 0; i < ntimes; i++){
      data_i[i][0][0]=data[i][0];
      data_i[i][0][1]=data[i][1];
      data_i[i][0][2]=data[i][2];
    }

Do you think here that data_i[i][0][0] is the right syntax ? Is this the better way to do this transfer or copy of information ? Thanks

A: 

For 3 floats at a time that's all nice and good.

If you think your program is running slow, make sure the copying is responsible for the slowness before trying to "make it better".

pmg
+2  A: 

If you always calloc exactly 1 array, you can only index the array pointer with 0 anyway. What's the reason for not taking float* then and omitting the one index step?

typedef float *el_i;
el_i data_i[NTIMES];
for (i = 0; i < ntimes; i++) data_i[i] = (el_i)calloc(1, sizeof(float[3]));

for (i = 0; i < ntimes; i++){
  data_i[i][0]=data[i][0];
  data_i[i][1]=data[i][1];
  data_i[i][2]=data[i][2];
}

I don't really see the whole purpose of this.

Johannes Schaub - litb
with gcc I get errors when doing this "incompatible types in assignment"
Werner