int arr[3][3];
int i, sum=0;
i = 0;
while(i<3)
{
sum += ((arr+i)+(i++));
}
printf(“sum:%d”, sum);
Options:
Sum of all elements in the matrix
Sum of alternate elements in the matrix
Sum of the elements along the principal diagonal
None of these
Answer: Sum of the elements along the principal diagonal
Explanation:
Observe that the unary operator ++ in the program is a post increment operator. Thus
the value of i gets incremented only after the execution of the statement.
Since arr is a 2-D array, arr represents a pointer to an array of pointers, where each
pointer in the array in turn points to a 1-D array(i^th pointer points to the starting location
of i^th row in the array). So, *(arr+i) represents the starting address of i^th row. This
implies *(arr+i)+i represents the address of i^th element in the i^th row, which is
basically a diagonal element.