In the piece of code, arr[ ][ ] is a 2D array and assume that the
contents of the 2D array are already filled up. What is stored in the
variable sum at the end of the code segment?
int arr[3][3];
int i, sum=0; i = 0; while(i<3) {
sum += * ( * (arr+i)+(i++)); }
printf(“sum:%d”, sum);
A) Sum of all elements in the matrix
B) Sum of alternate elements in the matrix
C) Sum of the elements along the principal diagonal
D) None
here c is the answer…
please explain the working is this line in detail:
sum += * ( * (arr+i)+(i++));
how does the outer pointer take the value inside the bracket and do further computations?
Pointers mcq doubt
hello @kani001
ok first see this
a+i gives address of ith block and *(a+i) will give u value on that block.
so we can say
a[i]= *(a+i)
now see this->
the inner *(arr+i) , we can write it as arr[i]
so now this turned into
sum+= * (arr[i]+ (i++) )
again this using above logic we can write
*(arr[i] + (i++) ) = arr[i][i++]
so
finally it become
sum+=arr[i][i++]
which is nothing but diagonal sum