+ 3

C++ confusion in array....

#include<iostream> using namespace std; int main() { char arr[]={'S','o','l','o','l','e','a','r','n'}; int num[]={1,2,3,4,5}; cout<<arr; // output: sololearn cout<<num; // output: memory address } In the above code please explain me why, cout<<arr; & cout<<num; gives different output. one gives memory address and other prints full array why so!!? also every time we run program after sololearn being printed some other characters also comes!!? what's this mean? https://code.sololearn.com/cdVVoJQFojbw/?ref=app

28th Jul 2018, 1:34 AM
Suraj Jha
Suraj Jha - avatar
5 odpowiedzi
+ 5
The << operator is overloaded for char pointers. It would output the contents of whatever is stored in the memory starting from the first address until a null character is met ('\0'). https://stackoverflow.com/questions/29188668/cout-and-char-address As for why some garbage values are found at the end, please read through this: http://archive.oreilly.com/oreillyschool/courses/cplusplus1/cplusplus106.html
28th Jul 2018, 2:56 AM
Hatsy Rei
Hatsy Rei - avatar
+ 4
Constructor Woops! Fixed. I swear it's not the first time I messed that up...
28th Jul 2018, 1:29 PM
Hatsy Rei
Hatsy Rei - avatar
+ 3
Hatsy Rei has already answered you. Just one thing to correct. We overloaded the operator <<, not the std::cout.
28th Jul 2018, 6:35 AM
Constructor
Constructor - avatar
+ 2
thanking you all guyzz Hatsy Rei, Constructor and Rohan Vijayvergiya 😊😊
28th Jul 2018, 12:18 PM
Suraj Jha
Suraj Jha - avatar
+ 1
because an array cant be called by name without indexing . now for char array it takes it as a string so it prints it with name but for a int array it gives error or prings out the memory address of the array . that is why cout arr prints sololearn but cout num gives memory address.
28th Jul 2018, 2:54 AM
Rohan Vijayvergiya
Rohan Vijayvergiya - avatar