Q7 from pointers

Q­7 In the piece of code, arr[ ][ ] is a 2­D array and assume that the
contents of the 2­D 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);

Q­4 What is the problem with the following code #include<stdio.h> int main() { int *ptr = (int *)malloc(sizeof(int)); ptr = NULL; free(ptr); } A) Dangling Pointer B) Memory leak C) The program may crash as free() is called for NULL pointer. D) Compiler Error

we can access any element arr[i][j ] of the array using the pointer expression (( arr + i) + j ).

In this code *(arr+i)+(i++) this will results in the addresses where i can be 0,1,2 in the loop
so it will result into
C) Sum of the elements along the principal diagonal

It will not compile as it will give the compilation error for line: j=k=&a; because there’s no implicit conversion from void* to type * in C++, so it will give you the error invalid conversion from ‘void*’ to ‘int*’

let’s understand this through an example

arr={{1 2 3 }{4 5 6}}
this is an 2D array or we can say array of arrays

arr is containing address of its first element which is also an array {1 2 3} (arr[0]) and arr[0] contains address of its first element which is arr[0][0]

if i write *(arr+i ) means we are at arr[i]

and if we write *(*(arr+i)+i++) means arr[i][i] which is diagonal element

i hope this help
if you have more doubts regarding this feel free to ask
if your doubt is resolved mark it as resolved from your doubt section inside your course

i got your point that in ((arr+i)+i++) the inner i gives the row index and outer i is for column index, but my question is arr is the address for the arr[0][0] and we were taught that when we do *(any address) it gives the value at that address, then how *(arr+1) giving the address here since i tried printing it out and it gives address only.

actually 2d array is array of array
so arr[0] is also a array hence *(arr+1) gives you address of arr[1] which is second row

this picture may clear this more
hope this picture will help you to understand well

Screenshot from 2020-05-03 11-25-10

This helped, thanks.