What will be the output of the following C code? #include void main() { int…
2024
What will be the output of the following C code? #include
void main()
{
int arr[5] = {10, 20, 30, 40, 50};
int *p = (int*) (&arr + 1);
printf("%d %d", *(arr + 1), *(p-1));
}
- A.
10 50
- B.
20 50
- C.
30 40
- D.
20 40
Attempted by 192 students.
Show answer & explanation
Correct answer: B
Answer: 20 50
Explanation:
The array arr has elements: 10, 20, 30, 40, 50.
*(arr + 1) accesses the element at index 1, which is 20.
&arr has type pointer-to-array (int (*)[5]). Adding 1 to &arr moves past the entire array (by 5 ints).
Casting (&arr + 1) to int* yields a pointer to one-past-the-last element (arr + 5). So p points to arr[5].
Therefore p-1 points to arr[4], which is 50.
Putting it together, the printf prints the values 20 and 50, so the output is "20 50".
A video solution is available for this question — log in and enroll to watch it.