+ 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
5 ответов
+ 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
+ 4
Constructor Woops! Fixed. I swear it's not the first time I messed that up...
+ 3
Hatsy Rei has already answered you. Just one thing to correct. We overloaded the operator <<, not the std::cout.
+ 2
+ 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.