Hi everyone,
I have a problem in returning an unsigned char* from a function.
In the main function:
string name = "yabansu";
unsigned char* arr1 = name.c_str();
cout << arr1 << endl; //Correctly prints "yabansu"
Let's say I wrote a function called StringToBytes for doing the same thing above as the following:
unsigned char* StringToBytes(s tring s)
{
cout << (unsigned char*) s.c_str() //Correctly prints "yabansu"
return (unsigned char*) s.c_str();
}
Then I call this function in the main() as below;
string name = "yabansu";
unsigned char* arr2 = StringToBytes(n ame);
cout << arr2 << endl; //prints weird characters instead of "yabansu"
As you see, nothing is different and the string("yabansu ") can be properly displayed within the StringToBytes() function.
What could be the reason? Thanks...
I have a problem in returning an unsigned char* from a function.
In the main function:
string name = "yabansu";
unsigned char* arr1 = name.c_str();
cout << arr1 << endl; //Correctly prints "yabansu"
Let's say I wrote a function called StringToBytes for doing the same thing above as the following:
unsigned char* StringToBytes(s tring s)
{
cout << (unsigned char*) s.c_str() //Correctly prints "yabansu"
return (unsigned char*) s.c_str();
}
Then I call this function in the main() as below;
string name = "yabansu";
unsigned char* arr2 = StringToBytes(n ame);
cout << arr2 << endl; //prints weird characters instead of "yabansu"
As you see, nothing is different and the string("yabansu ") can be properly displayed within the StringToBytes() function.
What could be the reason? Thanks...
Comment