Discussion
What will be the output of the program assuming that the array begins at the location 1002 and size of an integer is 4 bytes?
#include <stdio.h>
int main()
{
int a[3][4] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
printf("%u, %u, %u\n", a[0]+1, *(a[0]+1), *(*(a+0)+1));
return 0;
}
Answer: C
Correct answer: 1006, 2, 2
Take the three arguments in turn. a[0] is a pointer to the first row's first element, at 1002, and adding 1 advances it by sizeof(int), giving 1006.
*(a[0]+1) dereferences that, yielding a[0][1], which is 2. And *(*(a+0)+1) is the same expression written with explicit pointer arithmetic - a+0 is a, *(a+0) is a[0] - so it too gives 2.
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