I was simply using copy function in c++ for copying a string into an array. But the result shown for the following code contain garbage values:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s;
char t[10];
s = "Hello";
s.copy(t, s.length());
cout << t;
return 0;
}
Output: Hellov._Sï
Whereas, if I do the same thing in a different way. I get the right output.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s = "Hello";
char t[10];
s.copy(t, s.length());
cout << t;
return 0;
}
Output: Hello
Can someone explain why is this happening ? I am relatively new to c++.