Open In App

C | Arrays | Question 4

Output of following program?




#include<stdio.h> 
    
int main() 
  int a[] = {1, 2, 3, 4, 5, 6}; 
  int *ptr = (int*)(&a+1); 
  printf("%d ", *(ptr-1) ); 
  return 0; 
}

(A) 1
(B) 2
(C) 6
(D) Runtime Error

Answer: (C)
Explanation: &a is address of the whole array a[]. If we add 1 to &a, we get “base address of a[] + sizeof(a)”. And this value is typecasted to int *. So ptr points the memory just after 6 is stored. ptr is typecasted to “int *” and value of *(ptr-1) is printed. Since ptr points memory after 6, ptr – 1 points to 6.
Quiz of this Question

Article Tags :