What will be the output of the following C program?
int a[] = {10,20,30};
int *p = a;
p++;
printf("%d", *p);
Options
-
A.
10
-
B.
20
-
C.
30
-
D.
More than one of the above
-
E.
None of the above
Correct Answer (Detailed Explanation is Below)
B.
20
Detailed Explanation
Correct Answer: 20
Initially, p = a, so p points to the first element, which is 10.
When p++ is executed, the pointer moves to the next integer element of the array. It therefore points to 20.
Thus, *p gives 20.
Important: Pointer arithmetic depends on the data type. For an int *, p++ moves the pointer to the next integer element, not merely one byte forward.