in Programming in C edited by
5,861 views
2 votes
2 votes
include <stdio.h>
int main()
{
    int a[][3] = {1, 2, 3, 4, 5, 6};
    int (*ptr)[3] = a;
    printf("%d %d ", (*ptr)[1], (*ptr)[2]);
    ++ptr;
    printf("%d %d\n", (*ptr)[1], (*ptr)[2]);
    return 0;
}



(a) 2 3 5 6

(b) 2 3 4 5

(c) 4 5 0 0

(d) none of the above

in Programming in C edited by
5.9k views

2 Comments

Is A) given answer??
0
0
correct..
0
0

2 Answers

6 votes
6 votes
Best answer
include <stdio.h>
int main()
{
    int a[][3] = {1, 2, 3, 4, 5, 6};
    int (*ptr)[3] = a;
    printf("%d %d ", (*ptr)[1], (*ptr)[2]);
    ++ptr;
    printf("%d %d\n", (*ptr)[1], (*ptr)[2]);
    return 0;
1 2 3
4 5 6
 int (*ptr)[3] = a; // here ptr is pointer to an array of 3 integers.

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

So, (*ptr)[1]=*(2004)=2 and (*ptr)[2]=*(2008)=3

Now ++ptr is updating pointer to next row. So, Now ptr pointing to address 2000+3*4=2012

So, Now So, (*ptr)[1]=*(2016)=5 and (*ptr)[2]=*(2020)=6

selected by

2 Comments

@srestha,here if i want to point to address of 2..then what should i write??
0
0
&(*ptr)[1]
1
1
0 votes
0 votes

The correct answer is (a) 2 3 5 6.

Explanation:

  • -> Initially, ptr points to the first array in a, so (*ptr)[1] prints the second element (2) and (*ptr)[2] prints the third element (3).
  • -> After incrementing ptr, it now points to the second array in a, so (*ptr)[1] prints the second element (5) and (*ptr)[2] prints the third element (6)