How to assign a string to char *pw in c++
like...
char *pw = some string
??
For constant initialization you can simply use
const char *pw = "mypassword";
if the string is stored in a variable, and you need to make a copy of the string then you can use strcpy() function
char *pw = new char(strlen(myvariable) + 1);
strcpy(pw, myvariable);
// use of pw
delete [] pw; // do not forget to free allocated memory
If you just want to assign a string literal to pw
, you can do it like char *pw = "Hello world";
.
If you have a C++ std::string
object, the value of which you want to assign to pw
, you can do it like char *pw = some_string.c_str()
. However, the value that pw
points to will only be valid for the life time of some_string
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With