Pointers mcq doubt

pointers mcq.Can I please get a detailed explanation of Q.6,7,8…

HI Shubham, for Q6, in the for loop ptr is initialized as:

ptr = arr

i.e. now ptr is pointing to starting element of arr i.e. arr[0], now ptr++ means ptr points to the value next to ptr. value next to ptr i.e a[0] is a[1] i.e. ptr++ takes ptr to arr[1] from arr[0]. Now *(ptr+i) means the value which is i steps forward than where arr currently is.
i.e.

at 1st iteration:
ptr = arr i.e. arr[0], i = 0 ;
*(ptr+i) means a[0+i] => arr[0] => 0

at 2nd iteration:
ptr = arr++ i.e. arr[1], i = 1 ;
*(ptr+i) means a[1+1] => arr[2] => 2

at 3rd iteration:
ptr = arr++ i.e. arr[2], i = 2 ;
*(ptr+i) means a[2+2] => arr[4] => 4

Hence 024 gets printed.

7th question is similar to this one so pls try it again after comprehending this.

In Q8, k++ will pose a problem because it is a pointer to void and you cannot perform arithmetic operations on pointers to void.

Hope this helps :slight_smile:

1 Like

Hey Shubham,
As you are not responding to this thread, I am marking your doubt as Resolved for now. Re-open it if required.

Please mark your doubts as resolved in your course’s “ Ask Doubt ” section, when your doubt is resolved.