char el[3] = myvector[1].c_str();
myvector[i]
is a string with three letters in. Why does this error?
It returns type char* which is a pointer to a string. You can't assign this directly to an array like that, as that array already has memory assigned to it. Try:
const char* el = myvector[1].c_str();
But very careful if the string itself is destroyed or changed as the pointer will no longer be valid.
Because a const char *
is not a valid initializer for an array. What's more, I believe c_str
returns a pointer to internal memory so it's not safe to store.
You probably want to copy the value in some way (memcpy
or std::copy
or something else).
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