0

Can anyone explain?

https://code.sololearn.com/ctYxerPj6ew6/?ref=app one of the C++ challenges on here, hard coded it and tried to understand but canā€™t work out why it coutā€™s 10?? Not sure what c#.#.# etc is doing? Or what happens when the ={#,#,#,#} is called, help explaining is more than welcome. Cheers

10th Jan 2018, 8:48 PM
Lee Wright
Lee Wright - avatar
3 Answers
+ 1
im glad to help, to tell you the truth, the naming and ordering of the varibles got me confused for a bit there too
10th Jan 2018, 9:24 PM
capsloth
capsloth - avatar
+ 2
Thank you so much for the quick and detailed response. Never seen anything like that before and couldnā€™t understand what was going on at all. Kudos to you cheers, tried a few different combinations and starting to get it now šŸ‘ŒšŸ»
10th Jan 2018, 9:19 PM
Lee Wright
Lee Wright - avatar
+ 1
So we know that C is a structure, containig two differents structures, like this struct C{ A a; B b; }; To acess each member we use the following C.a to get to the elements to the A struct C.b to get to the elements to the B struct We want now get to the variables, so we continue, t he sintaxe its the same, since A and B are struct too C.a.a C.a.b C.b.a C.b.b its important to know that each C struct will be put in the memory as declared: so you have C{A a; B b} = C{int a, float b; int b, float a} Now when the assigment its done: C c1 ={1,2,3,4}; its the same as: c1.a.a = 1 c1.a.b = 2 c1.b.b = 3 c1.b.a = 4 C c2 ={5,6,7,8}; its the same as: c2.a.a = 5 c2.a.b = 6 c2.b.b = 7 c2.b.a = 8 so c1.b.a + c2.a.b = 4 + 6 = 10
10th Jan 2018, 9:20 PM
capsloth
capsloth - avatar