How to implement foreach loop on multidimensional array in C? -


i have found macro here, @johannes schaub used array. tried apply multidimensional array got warning:

initialization incompatible pointer type [enabled default]|

#define foreach(item, array) \     for(int keep = 1, \             count = 0,\             size = sizeof (array) / sizeof *(array); \         keep && count != size; \         keep = !keep, count++) \       for(item = (array) + count; keep; keep = !keep)  double daysearthsun[][10] = {     //            0                                                         1                       2                       3                       4                       5                       6                       7                       8                       9     //          jdtdb,            calendar date (tdb),                      x,                      y,                      z,                     vx,                     vy,                     vz,                     lt,                     rg,                     rr,     {2305447.500000000, /*"a.d. 1600-jan-01 00:00:00.0000",*/ -2.568497981915648e-01,  9.438245451677045e-01,  6.410938668761658e-04, -1.684598702834566e-02, -4.667597482526307e-03, -4.906040833845624e-06,  5.649322014152373e-03,  9.781497849989120e-01, -8.026158042429985e-05},     {2305448.500000000, /*"a.d. 1600-jan-02 00:00:00.0000",*/ -2.736541829631095e-01,  9.390104932363517e-01,  6.360724040092633e-04, -1.676196451434489e-02, -4.960286450217222e-03, -5.142448255071298e-06,  5.648881285390255e-03,  9.780734751792867e-01, -7.236940265538736e-05} };  void printsoe(){     double distance, velocity, km, km_2, speed;     file *f;     foreach(int *soe,                 daysearthsun) {         distance = sqrt( soe[1]*soe[1] + soe[2]*soe[2] + soe[3]*soe[3] ); // units: au-d         velocity = sqrt( soe[4]*soe[4] + soe[5]*soe[5] + soe[6]*soe[6] ); // units: au-d         km = (149597870.700*distance); // km/day         speed = (149597870.700*velocity); // km/day         km_2 = 25902068370*soe[7]; // e-s distance: light day km         printf("\n\n%f km , %f km/day\n", km, speed);         printf("distance based on light: %f km/day\n\n", km_2);         f = fopen("output.txt", "a");         fprintf(f, "%f, %f,", km, speed );     }     fclose(f); } 

there's 2 errors here.

the first have type mismatch. have int *soe attempting assign double [] (which decays double *) it. change double *soe.

the second error in macro:

for(item = (array) + count; keep; keep = !keep) 

it looks you're attempting assign element of array item, that's not what's happening. pointer addition on array, fail dereference it.

either add dereference:

for(item = *((array) + count); keep; keep = !keep) 

or use array element operator:

for(item = (array)[count]; keep; keep = !keep) 

Comments

Popular posts from this blog

javascript - Create a stacked percentage column -

Optimising Firebase database by automatically overwriting data -

javascript - Angular UI-Grid customTemplate directive causing rows to load slowly/? -