c - assigning a 2D array element to single pointer -
i trying learn c. new c programming. have following function.
/*da pointer 2d array*/ void normalizecols(float* dmu, float* dsigma, float* db, float* da, int n){ int col, row; for(col=0; col < n; col++) /*step 1: calculating mean*/ float tempmu = 0.0; dmu = &tempmu; (row=0; row < n; row++){ /*i adding elements of column*/ dmu += *(*(da+row)+col); //error: operand of * must pointer } /*dividing dmu number of dimension(square matrix)*/ dmu /= (float) n; //error: expression must have arithmetic or enum type //more code here } }
i trying find mean of column. 2 errors have commented in above snippet. how should fix this?
if know matrices square (i.e. row-length n
number of rows), addressing manually.
the inner loop becomes:
/*step 1: calculating mean*/ float tempmu = 0; (row=0; row < n; row++){ /*i adding elements of column*/ tempmu += da[col * n + row]; } /*dividing dmu number of dimension(square matrix)*/ tempmu /= (float) n;
also, make input arguments const
make clearer, , switch int
size_t
.
of course, make sure accesses in proper order (either row-major or column-major) otherwise you'll horrible cache thrashing.
Comments
Post a Comment