1
Can anyone explain to me what "a[i]+i" means? When i=0 the pointer points to 2. But when i=1 shouldn’t be a[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;
}
*(a[i]+i)
is an exotic way of sayinga[i][i]
;a[1]
not 4 plus a pointer pointing to the array{6,8}
;a[1]+1
points to the 8;*(a[1]+1)
is the 8.– JJoao
I think I figured it out, so if there was still another line in the matrix, p.ex {10,12}, *(a[2]+1) pointed to 10 and *(a[2]+2) pointed to 12, right?
– whoami
Try it. It’s almost:
(a[2]+1)
points to the 12;*(a[2]+1)
is the 12 .*(a[2]+0)
would be the 10.– JJoao
Exactly, it would only be a[2]+2 if there were another column.
– whoami