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; } |
chevron_right
filter_none
(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
Recommended Posts:
- C | Arrays | Question 14
- C | Arrays | Question 13
- C | Arrays | Question 12
- C | Arrays | Question 6
- C | Arrays | Question 2
- C | Arrays | Question 5
- C | Arrays | Question 7
- C | Arrays | Question 14
- C | Arrays | Question 9
- C | Arrays | Question 1
- C | Arrays | Question 10
- C | Arrays | Question 11
- C | Arrays | Question 3
- Java | Arrays | Question 3
- Java | Arrays | Question 6



