structure using pointer | Sololearn: Learn to code for FREE!
¡Nuevo curso! ¡Todo programador debería aprender IA Generativa!
Prueba una lección gratuita
0

structure using pointer

https://code.sololearn.com/crYS56nm3N6P/#c couldnt print the input values correct the places i have made mistakes

23rd Mar 2020, 7:45 AM
Reiner
Reiner - avatar
4 Respuestas
+ 4
The mistake was that you used tem->value for printing instead of tem[i].value. Therefore you only printed the first element of the temp array. And because you used i=1 in the for loops you did start with the second element of temp. This means that the first element stayed untouched but you only printed the first element. Therefore the code didn't work at all. Here's a fixed version: https://code.sololearn.com/cj8jIrePkE1q/?ref=app P. S. i shouldn't be a global variable but defined in the for loop like this: for (int i = 0; i < n; ++i) { .... }
23rd Mar 2020, 8:20 AM
Aaron Eberhardt
Aaron Eberhardt - avatar
+ 4
reiner A yes it is right that there's no rule to start with 0. But in this case not starting with 0 made it harder to debug. Also starting with 1 will make one array element unused and therefore waste memory. An array with 10 elements will just provide 9 if you start at 1. That's why almost every programmer starts at 0.
25th Mar 2020, 8:02 AM
Aaron Eberhardt
Aaron Eberhardt - avatar
+ 1
i made a miskate "tem->name" here only i=1 also works fine no issues on that, there is no rule to start with '0'
25th Mar 2020, 5:36 AM
Reiner
Reiner - avatar
+ 1
thanks for such a clarification will go through it
25th Mar 2020, 10:58 AM
Reiner
Reiner - avatar