+ 6

Explaination Needed for C++ code.

I am learning c++. and when I was in array and function section. I couldn't elaborate a code with array and function.It is giving some odd result when I have declared a array of 3 and in function it's size is of 4 and some random value is printing out. I have attached the code. Please see if you can give answer. https://code.sololearn.com/cJuJQaz2i65v/#cpp

1st Jan 2018, 3:34 PM
Akash Pal
Akash Pal - avatar
3 Respuestas
+ 5
your function iterates 4 times i.e. 0,1,2,3. since the size you defined is only 3, it uses elements @ position 0,1,2 and not 3. since no element is defined at index 3, a random garbage value is used. Thus, for the correct working of the program, replace the second parameter by 3.
1st Jan 2018, 3:41 PM
Vihang Puranik
+ 5
Yeah, I know that @Nafis but I wanted to know why and what it is returning a 4th value. Thanks to @Vihang I got it.
1st Jan 2018, 3:52 PM
Akash Pal
Akash Pal - avatar
+ 4
3 is the size of the array..so pass 3 as size in printArray function
1st Jan 2018, 3:41 PM
Md. Nafis Ul Haque Shifat
Md. Nafis Ul Haque Shifat - avatar