1
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?

Jin
  • 1,902
  • 3
  • 15
  • 26
  • 1
    The first problem is that `ar` is a pointer to `int` i.e. a single-dimensional array, something that should be very clear from your error messages. You need it to be a pointer to arrays of `int`, or a pointer to pointer to `int` (depending on what the argument actually is). Please try to create a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve) and show us. – Some programmer dude Aug 08 '16 at 05:40
  • 2
    Shouldn't that be `int **ar` in the first line? – Wickramaranga Aug 08 '16 at 05:41
  • @Wickramaranga Not if the actual variable being passed is an array of arrays. – Some programmer dude Aug 08 '16 at 05:44
  • @JoachimPileborg Okay. I will keep that in mind! – Jin Aug 08 '16 at 05:53

0 Answers0