282 views
2 votes
2 votes

What will be the output of the C program?

#include<stdio.h>
int main()
{
    int a[][4] = {0, 1, 2, 3, 4, 5, 6, 7};
    int (*ptr)[4] = a;
    printf("%d %d ", (*ptr)[0], (*ptr)[1]);
    ++ptr;
    printf("%d %d\n", (*ptr)[0], (*ptr)[1]);
    return 0;
}
  1. $0 \ 1 \ 3 \ 4$
  2. $0 \ 1 \ 4 \ 5$
  3. $0 \ 1 \ 2 \ 3$
  4. $0 \ 1 \ 6 \ 7$

 

1 Answer

Best answer
6 votes
6 votes
0 1 2 3
4 5 6 7
 int (*ptr)[4] = a; // here ptr is a pointer to an array of 4 integers.

At first ptr is pointing to first row. Say address of $1$st row is $1000.$

So, (*ptr)[0] = *(1000 + 0*4) = *(1000) = 0 and (*ptr)[1] = *(1000 + 1*4) = *(1004) = 1

Now ++ptr is updating pointer to next row (increment will be the sizeof (*ptr) which here is an integer array of size 4). So, Now ptr is pointing to address $1000+4*4 = 1016$

So, Now (*ptr)[0] = *(1016 + 0*4) = *(1016) = 4 and (*ptr)[1] = *(1016 + 1*4) = *(1020) = 5

So, the correct answer is $(B).$

selected by
Answer:

Related questions

7 votes
7 votes
2 answers
1
3 votes
3 votes
1 answer
2
gatecse asked Aug 9, 2020
227 views
The cut vertices in the given graph areE and FA, C and DC, E and FB and C
1 votes
1 votes
1 answer
3
1 votes
1 votes
1 answer
4
gatecse asked Aug 9, 2020
185 views
What is the value of the postfix expression $8 \ 7 \ 2 \ 4 \ + \ – \ *?$: