🔒 Locked
string to char*
Advertisement
Original Post
Are you trying to copy the string to an array of chars, or get a pointer to it?
If you''re trying to copy it, you must do something like this...
char array[100];
strcpy (array, str.c_str ());
If you''re trying to get a pointer to it, I recommend against it, as the pointer you get is not necessarily going to be valid for any length of time.
Helpful links:
How To Ask Questions The Smart Way | Google can help with your question | Search MSDN for help with standard C or Windows functions
If you''re trying to copy it, you must do something like this...
char array[100];
strcpy (array, str.c_str ());
If you''re trying to get a pointer to it, I recommend against it, as the pointer you get is not necessarily going to be valid for any length of time.
Helpful links:
How To Ask Questions The Smart Way | Google can help with your question | Search MSDN for help with standard C or Windows functions
Usually you would use abc.c_str() to compare against another string, and being const char* is best for a compare.
If you want to make a seperate copy of the string, you do just that you make a copy:
Now you have a copy thats independant of abc.
edit: love these simple questions
its easy for lots of people to answer
[edited by - atavist on June 11, 2002 12:29:43 PM]
If you want to make a seperate copy of the string, you do just that you make a copy:
std::string abc = "Some string";
char *p;
int len;
len = abc.length();
p = new char[len+1];
strcpy(p,abc.c_str());
//later on make sure to
delete [] p;
Now you have a copy thats independant of abc.
edit: love these simple questions
[edited by - atavist on June 11, 2002 12:29:43 PM]
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement