How do you change a string to a char* ?
Printable View
How do you change a string to a char* ?
Well in C++ a string is an array of char. Do you mean the <string> class. To make it a c string use :
Code:string mystr = "string";
mystr.c_str(); // makes it a c string
The problem I have is I got a VAR that is a string (<string>) and I need to pass it to a function that requires a char* or a LPCTSTR. I figure I could loop through the array and passing each char to a temp char array, but I figure there must be an easier way.
Well use
Code:mystr.c_str();
Tried that, said it could not convert from string (acually a long class of char*) to a char*. Maybe I am using it wrong. Example?
Post some code?
It actually makes no differnce until I get my class working.
http://161.58.186.97/showthread.php?s=&threadid=79765
string.c_str() returns a const char* pointer to the data string, so you can't change the contents. If you actually want to obtain the data, you need to use strcpy on it. This is another good reason for using const well in your code.
DOH!!! :eek:
Thats why it dont work. Man I hate making stupid mistakes like that
I really wish they'd make things like that obvious :rolleyes: Would've made my life a lot easier :(
Anyway, I'm off to look at your other thread now...