What is logic behind this code plz explain !!!!! | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What is logic behind this code plz explain !!!!!

//matrix important result #include <stdio.h> #include <conio.h> int main() { int arr[2][3]={2,3,4,5,6,7,8,9,10,11,12};//2 row 3 column printf("%d",(*(*(arr+1)))); return 0; }

9th Mar 2020, 8:43 AM
Onkar Ambuse
Onkar Ambuse - avatar
11 Answers
+ 1
You're assigning a 1td array to a 2d array which will kind of pop you some errors why not run the code on codeplayground and see what you get
9th Mar 2020, 8:52 AM
✳AsterisK✳
✳AsterisK✳ - avatar
+ 1
Yes code runs and gives output =5
9th Mar 2020, 8:54 AM
Onkar Ambuse
Onkar Ambuse - avatar
+ 1
For a 2x3 array, you have too many values in array initializer. IIRC there should only be 6 values.
9th Mar 2020, 9:23 AM
Ipang
+ 1
I was only explaining what the compiler said about the code. Apart from having too many initializer values, the compiler also said there should be curly brackets within the array initializer to clarify row and column.
9th Mar 2020, 9:30 AM
Ipang
+ 1
No problem buddy 👌
9th Mar 2020, 9:34 AM
Ipang
+ 1
Your array: {{2, 3, 4}, {5, 6, 7}} First row: {2, 3, 4} Second row: {5, 6, 7} *(arr) refers to first row *(*(arr)) refers to first column at first row *(arr + 1) refers to second row *(*(arr + 1)) refers to first column at second row
9th Mar 2020, 10:01 AM
Ipang
+ 1
Thank you all of you I got a proper answer now .
9th Mar 2020, 12:02 PM
Onkar Ambuse
Onkar Ambuse - avatar
0
But it is running and getting output=5!!!
9th Mar 2020, 9:25 AM
Onkar Ambuse
Onkar Ambuse - avatar
0
Ohk thank you so much
9th Mar 2020, 9:33 AM
Onkar Ambuse
Onkar Ambuse - avatar
0
#include <stdio.h> #include <conio.h> int main() { int arr[2][3]={2,3,4,5,6,7}; printf("%d",(*(*(arr+1)))); return 0; } Output =5 Run this code now and check ouput
9th Mar 2020, 9:37 AM
Onkar Ambuse
Onkar Ambuse - avatar
0
you can define int arr[2][3]={2,3,4,5,6,7,8,9,10,11,12}; as typedef int tripple[3]; tripple arr[2]={2,3,4,5,6,7,8,9,10,11,12}; it is array of two tripples so *(arr+1) is the same as arr[1] which is a pointer to {5,6,7} *(*(arr+1)) is dereferencing of the pointer to {5,6,7} which is 5 (pointer point to first begining of the array) printf outputs that 5 as result. According to the C standard it is allowed to intialize array (and other aggregate types) with less brace pairs than dimension of the array. In that case only enough initializers from the list are taken to account for the elements of the subaggregate (subdimension); any remaining initializers are left to initialize the next element of the aggregate (dimension) of which the current subaggregate is a part. In other words the next line is valid: int arr[2][3]={2,3,4,5,6,7,8,9,10,11,12} but compiler may show a warning message arr will contain the next values: {{2,3,4},{5,6,7}} But line: int arr[2][3]={{2,3,4,5,6},{7,8,9,10,11,12} would be not valid (number of brace pairs is the same as number of dimensions) compiler would give an error message
9th Mar 2020, 10:59 AM
andriy kan
andriy kan - avatar