Pls explain this code to me. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 9

Pls explain this code to me.

How does the code produce so unexpected output? https://code.sololearn.com/ctV53twJWpNh/?ref=app

22nd Oct 2017, 5:06 AM
Swapnil Srivastava
Swapnil Srivastava - avatar
2 Answers
+ 5
That's because the array has not been initialized, it has been declared, but it contains nothing. I couldn't quite explain why such memory dump is returned by the compiler, but it was bad. You need to have values stored within the array before you can actually request one from it, here's a modified version of your code: int main() { string x[]={"My name", " is John Doe,", " Nice to meet you"}; cout << x[2]; return 0; } Hth, cmiiw
22nd Oct 2017, 8:40 AM
Ipang
+ 5
garbage value is always unexpected
22nd Oct 2017, 6:59 PM
Infinity
Infinity - avatar