2 2 votes Find the output printed by this program. Pls. Explain the Execution of this program in detail. Thanks Programming in C programming-in-c programming output + – yogi_p 2.8k views answer comment Share Follow Print See all 22 Comments 22 22 Comments reply MiNiPanda commented Jan 22, 2018 reply Follow flag Is is printing the sum of all elements? 0 0 replyShare yogi_p commented Jan 22, 2018 reply Follow flag arr[0][0[0]+arr[0][0][1]+arr[1][0][0]+arr[1][0][1]=1+2+5+6 Therefore,the answer is 14. I am not getting how we reached here ? If you get it. plz explain. 0 0 replyShare Ashwin Kulkarni commented Jan 22, 2018 reply Follow flag Yes please see that middle loop is incremented by 2. Hence first time a[0][0][0] = 1 + a[0][0][1] = 2 then middle loop is incremented by directly 2. hence that won't execute. Then outer loop will increment by 1 hence we reach a[1][0][0] = 5 + a[1][0][1] = 6 $Total = 1+2+5+6 = 14$ 0 0 replyShare yogi_p commented Jan 23, 2018 reply Follow flag See starting address of arr (i.e 100 ) is passed to p. Now, how we proceed further on ? What would be the address assigned to array containing 1,2, .......? Would it be contiguous ( If yes how we proceed further?) 0 0 replyShare debanjan sarkar commented Jan 23, 2018 reply Follow flag here ptr is a pointer to 1-D array and p is pointer to 2-D array...how we can assign p to ptr? wont there be compilation error? 0 0 replyShare vishal chugh commented Jan 23, 2018 reply Follow flag Here, "arr" points to the base address of the complete 3-d array, "p" points to base address of the first block 0f 2-d arrays which is {{1,2}, {3,4}}. Now, in the first iteration: (i) Outermost Loop: "ptr" is equal to 'p', thus, 'ptr' points to address of first element of 2-d array which is 1. (ii) Middle Loop: 'ptr' still points to '1' (iii)Innermost Loop: '1' is added to the value of sum in first iteration, then value of 'ptr' is incremented by '1' and thus now points to '2' and this value is added, So, at the end of first iteration of complete loop. The value of sum is 3. (iv)Now, in the second iteration value of 'ptr' is made equal to 'p+1'. Now since 'p' pointed to a complete block of 2-d array 'p+1' will point to the block {{5,6},{7,8}}. The inner loops will have the same iteration and thus '5' and '6' will be added to the sum. Hence, sum = 1+2+5+6 = 14 1 1 replyShare Ashwin Kulkarni commented Jan 23, 2018 reply Follow flag @debanjan ptr is a pointer which points to base address of p or any incremented address od p. 0 0 replyShare debanjan sarkar commented Jan 23, 2018 reply Follow flag just check this out guys... 0 0 replyShare debanjan sarkar commented Jan 23, 2018 reply Follow flag @ashwin i didnt get u..can you please elaborate? 0 0 replyShare Ashwin Kulkarni commented Jan 23, 2018 reply Follow flag gcc gives warning for it and correctly calculated 14. 0 0 replyShare debanjan sarkar commented Jan 23, 2018 reply Follow flag its not warning...its showing compilation error and it is not running... 0 0 replyShare yogi_p commented Jan 23, 2018 reply Follow flag @vishal chugh "p" points to base address of the first block 0f 2-d arrays which is {{1,2}, {3,4}} how we got to know about it ? p=arr so shoudln't p contain 100 ? 0 0 replyShare yogi_p commented Jan 23, 2018 reply Follow flag Can anyone explain stepwise execution of this program, in terms of what address is reached when ? So that i get a clear picture of its execution. 0 0 replyShare Ashwin Kulkarni commented Jan 23, 2018 reply Follow flag @yogi_p @debanjan see this. I have printed addresses and statement at each execution hope you will get it! 1 1 replyShare MiNiPanda commented Jan 23, 2018 reply Follow flag @yogi_p I made the illustration..see if it helps you.. 1 1 replyShare vishal chugh commented Jan 23, 2018 reply Follow flag This video was a great help in making 3d array's concept clear to me. Hope this helps you. 2 2 replyShare Ashwin Kulkarni commented Jan 23, 2018 reply Follow flag and also interpret (*p)[2][2] this thing as - "p is a pointer to an array of [2][2]" This will clear the whole thing. 0 0 replyShare yogi_p commented Jan 23, 2018 reply Follow flag Thanks Everyone. There was a problem with my concept ,Got it cleared now. 1 1 replyShare Kiran Karwa commented Jan 24, 2018 reply Follow flag @ vishal @MiNiPanda @Ashwin I am confused. I will be highly grateful if any of u could help.. I am unable to understand few things. Here we know that a[2][2][2] is a 3-D array. int a[2][2][2] is same as int *a[2][2] is same as int **a[2] is same as int ***a. So here a is a triple pointer. Here in the above problem and places where I see they give a the address of the 1st element. But if a was containing the address of the 1st element directly than just de-referencing once will give us the element value,right? which is not the case in 3-D array where we have to de-reference thrice to print the value of an element. a shld contain &a[0] and further a[0] contains &a[0][0] and then finally a[0][0] containg &a[0][0][0]. therefore on dereferencing a[0][0] we get the element value. Then why is arr containing the 1st element address 100 here? Why when we are doing ptr=p then ptr is pointing to 1-D array whereas p is pointing to 2-D array. if ptr =p then both shld contain the same address and point to the same location,right? What am I missing out? PLease help me.. 1 1 replyShare MiNiPanda commented Jan 24, 2018 reply Follow flag arr is holding 100 it is true but this should not be interpreted as arr contains the address of an element(integer variable). See it as, arr is holding the address of the 1st element of the 3D array i.e. the 1st 2D array. Both of them may seem to be same but their type is different as you said. 0 0 replyShare Kiran Karwa commented Jan 24, 2018 reply Follow flag @ MiNiPanda Can u pls pls explain a bit more? If 100 is the 1st addr element and a contains 100 then why de-referencing it once won't give element value? 0 0 replyShare Kaluti commented Aug 19, 2018 reply Follow flag I think it is calculating sum of 1+2+5+6=14 0 0 replyShare Please log in or register to add a comment.