GATE CSE
First time here? Checkout the FAQ!
x
0 votes
549 views
#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

asked in Programming by (33 points) 2 2 3 | 549 views
ans  A?
Explanation?

int (*ptr)[3]  // ptr is a poinetr it stores 3 integer values.

int (*ptr)[3]=a; // it stores address of a.

(*ptr)[1] array of first element and (*ptr[2]) array of 2nd element.those are 2,3

(++ptr) it is  increased .it access 2nd dimensional array 

again (*ptr[1]) and (*ptr[2]) elements are 5,6

http://stackoverflow.com/questions/13910749/difference-between-ptr10-and-ptr10

int a[][3] = {1, 2, 3, 4, 5, 6};
int (*ptr)[3] = a;

Can u explain the structure for this 2 lines

2)ptr is a pointer to an array of storing 3 integer values.

ptr contains address of a.suppose address of a=1000 then 

 

 

it is giving result as 2,3,5,6 @vishal
@santhosh convert it as answer .  Perhaps Many of your comments should have to be converted to answers :)
Does the above structure for the 2D array is correct? . Because i was unable to visualise the 2D array structure with rows not mentioned.
two 2-d arrays a[0] and a[1]

in which a[0]---a[0][0] ,a[0][1],a[0[2] elements are 1,2,3

same as a[1]--a[1][0],a[1][1],a[1][2] elements are 4,5,6

1 Answer

+2 votes
Best answer
int *ptr[10];

This is an array of 10 int* pointers

int (*ptr)[10];

This is a pointer to an array of 10 int

  *(*a+1)   (*a)[1]  are two diffferent representation of a[0][1]

 

$int (*ptr)[3] $ // is nothing but  ptr[][]

$int (*ptr)[3]=a;$ // it stores address of a .

$(*ptr)[1]  $first element  // nothing but a[0][1]  which print 2
$(*ptr[2]) $ 2nd element. // // nothing but a[0][2] which print 3

$(++ptr)$ it is  increased .it access 2nd dimensional array 

$(*ptr)[1] $ first element  // nothing but a[1][1]  which print 5
$(*ptr[2])$  2nd element. // // nothing but a[1][2] which print 6

Output is $2,3,5,6$

http://stackoverflow.com/questions/13910749/difference-between-ptr10-and-ptr10

 

answered by Veteran (11.6k points) 6 20 86
edited by

Related questions



Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
Top Users Oct 2017
  1. Arjun

    23398 Points

  2. Bikram

    17078 Points

  3. Habibkhan

    8280 Points

  4. srestha

    6300 Points

  5. Debashish Deka

    5438 Points

  6. jothee

    4978 Points

  7. Sachin Mittal 1

    4772 Points

  8. joshi_nitish

    4352 Points

  9. sushmita

    3970 Points

  10. Rishi yadav

    3804 Points


Recent Badges

Popular Question sh!va
Popular Question sh!va
Regular Rishabh Gupta 2
Popular Question Sunil8860
Reader Rajesh Veeranki 2
Notable Question rahul sharma 5
Commentator Shivam Chauhan
Notable Question set2018
Nice Comment srestha
Notable Question set2018
27,325 questions
35,177 answers
84,123 comments
33,280 users