What will be the output of the following C program?
int a[] = {2,4,6,8};
int *p = &a[1];
printf("%d", p[2]);
Options
-
A.
4
-
B.
6
-
C.
8
-
D.
More than one of the above
-
E.
None of the above
Correct Answer (Detailed Explanation is Below)
C.
8
Detailed Explanation
Correct Answer: 8
The pointer p initially points to a[1], whose value is 4.
Pointer indexing follows the same rule as array indexing. Therefore, p[2] is equivalent to *(p+2).
Starting from a[1], moving two positions reaches a[3], whose value is 8.
Therefore, the output is 8.