Add the diagonal of an array (pointers)

1

Can anyone explain to me what "a [i] + i" means? When i = 0 the pointer points to 2. But when i = 1 should not be [1] +1 = 4 + 1 = 5? In practice it points to 8 ...

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int soma;
    int a[2][2] = {{2,4},{6,8}};
    printf("SOMA: %d\n", soma_diag(2, a));
}

int soma_diag(int n, int a[n][n])
{
    int soma=0, i;
    for(i = 0; i < n; i++)
        soma += *(a[i]+i);
    return soma;
}
    
asked by anonymous 07.04.2018 / 16:38

0 answers