 What is logic behind this code plz explain !!!!! | Sololearn: Learn to code for FREE!

0

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

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

+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

+1

Yes code runs and gives output =5

+1

For a 2x3 array, you have too many values in array initializer. IIRC there should only be 6 values.

+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.

+1

+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

+1

Thank you all of you I got a proper answer now .

0

But it is running and getting output=5!!!

0

0

#include <stdio.h> #include <conio.h> int main() { int arr={2,3,4,5,6,7}; printf("%d",(*(*(arr+1)))); return 0; } Output =5 Run this code now and check ouput

0

you can define int arr={2,3,4,5,6,7,8,9,10,11,12}; as typedef int tripple; tripple arr={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 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,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,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