Discussion
What will be the output of the program?
#include <stdio.h>
int main()
{
int arr[2][2][2] = {10, 2, 3, 4, 5, 6, 7, 8};
int *p, *q;
p = &arr[1][1][1];
q = (int*) arr;
printf("%d, %d\n", *p, *q);
return 0;
}
Answer: A
Correct answer: 8, 10
The initialiser fills the three-dimensional array in row-major order, so the eight values map to arr[0][0][0] through arr[1][1][1] in sequence: 10, 2, 3, 4, 5, 6, 7, 8.
The last of them, 8, is at arr[1][1][1], which is where p points. And q, cast from the array name, points to the very first element, which holds 10. So the output is 8, 10.
No comment is present. Be the first to comment.
Loading…
Post your comment
Please log in or register to post a comment.
Examination Categories
- SSC Exams
- Banking Exams
- Teaching Exams
- Civil Services Exams
- Railways Exams
- Engineering Recruitment Exams
- Defence Exams
- State Government Exams
- Police Exams
- Insurance Exams
- Nursing Exams
- Other Government Exams
- NRA CET
- PG Entrance Exams
- Campus Placement
- MBA Entrance Exams
- CUET and UG Entrance
- Judiciary Exams
- Regulatory Body Exams
- Software Development