+ 2

Hello, đŸ„ș🙏 please please explain me the code in description. I am really confused in this code (C Programming). 👇

Code: #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", (*ptr)[1], (*ptr)[2]); return 0; } Output: 2 3 5 6

24th Jan 2021, 6:06 PM
Mahima Rajvir Singh
Mahima Rajvir Singh - avatar
5 Answers
+ 7
Hello ma'am,I am trying to explain this code in the following points.hope you will understand. 1) Array "a" is declared with 3 columns and unknown rows.But initialisation of array "a" clarifies that it has 2 rows. [ { 1,2,3} , {4,5,6} ]. 2) int(*ptr)[3] says it is a pointer to an array of 3 int. 3) First printf will print 2nd and 3rd element of array "a". [Which is 2 3]. 4) Now this increment in pointer is quite tricky. As I said in 3rd point that it is not just a single int pointer , it is a pointer to an array of 3 int. [ It means that it is same as the name of an array. Because name of array is itself a pointer]. We know that increment of an pointer to array is not incremented to next element, it is incremented to its next row. Which means ++ptr jumped to its next row which is refers to 4th element. Means at ++ptr compiler assigns the address of 4th element of array to the pointer. Which means that 4th element of array "a" is referred as 1st element for "ptr" . 4) that's why 2nd printf will print 5,6.
24th Jan 2021, 6:54 PM
Rohit Rathod
Rohit Rathod - avatar
+ 4
Abol and Rohit Rathod Thank you so much for your great and easy explanations. I got everything you said! It was really helpful. 😊 Happy coding! 😄
24th Jan 2021, 7:00 PM
Mahima Rajvir Singh
Mahima Rajvir Singh - avatar
+ 1
Hello could you help me I have a java server I want to update the code to Java SE 15. And run it with eclipse or netbeans. Can you help me?
25th Jan 2021, 2:35 PM
Apti Amaev
Apti Amaev - avatar