Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Changing 'char *' to string

Status
Not open for further replies.

cadbilbao

Programmer
Apr 9, 2001
233
ES
Hello.

Until now, I've been using:
--------------//-----------------
char *MyValue = NULL;
MyValue = (char*)malloc(strlen(Another_Value)+1);
strcpy(MyValue,Another_Value);
-------------//------------------

Now, I want to change to the class 'string' (in order to avoid
memory leaks and buffer overflows).

I'm trying with:
------------------//--------------------
using std::string;
string string_MyValue;
string_MyValue = Another_Value
printf("\n string_MyValue = %s",string_MyValue);
-----------------//---------------------

But I do not get anything. Could anybody explain me what I am
doing wrong?

Thank you very much.
 
Try:
printf("\n string_MyValue = %s",string_MyValue.c_str());

printf isn't std::string aware. . .and if it were, it probably wouldn't use "%s".

you really ought to consider moving to cout and cin; but, even then you may have to add an overloaded

operator<<(const std::string&) depending on which implementation you're using.

Frank
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top