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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

string class

Status
Not open for further replies.

joeGrammar

Programmer
Jun 4, 2001
162
CA
Just say I have some thing like this

const char* temp = "test";

string value;

What is the proper way to assign the const char* to the string.

ie

value=temp will not work, what will?
 
#include <stdio.h>
#include <string>
using namespace std;

const char* g=&quot;test&quot;;
string h;
h.assign(g);
printf(h.c_str());

HTH, s-)

Blessed is he who in the name of justice and goodwill, sheperds the weak through the valley of darkness...
 
Hi there,

simplest way is simply to assign it!

const char* temp = &quot;Some string&quot;;
string str;
str = temp;

or you can assign in the constructor:

string str(temp);
or
string str = temp;

The other way (to get a c-string from a string) you need str.c_str() BTW.


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top