jwqwerty jwqwerty - 1 year ago 66
C Question

Error in finding column sum of 2 dim array in C

void sum_col(int *ar, int rows)
int c, r, total;
for (c = 0; c < 4; c++)
total = 0;
for (r = 0; r < rows; r++)
total += ar[r][c]; // error occurs
printf("%dth column sum: %d", c + 1, total);

I'm trying to define a function which prints column sums of a 2 dimensional array. But error occurs in
total += ar[r][c]
. What's wrong with this sentence?

Answer Source

It appears from your code that ar should be a 2-D array. If it is implemented as a "jagged" array then the parameter type should be int ** as a argument of function. like,

void sum_col(int **ar, int rows)
    // function code
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download