Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App

Related Articles

C | Arrays | Question 4

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

Output of following program?

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

My Personal Notes arrow_drop_up
Last Updated : 28 Jun, 2021
Like Article
Save Article
Similar Reads