How would you assign the value of a string variable to a char *?
How would you assign the value of a string variable to a char *?
string str;
char *ptr;
ptr = str.c_str();
"You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter
I tried that but my compiler gives me the error:
assignment to `char *' from `const char *' discards qualifiers
???
That means "You must explicitly cast this".
ptr = (char*)str.c_str();
Code:#include <cmath> #include <complex> bool euler_flip(bool value) { return std::pow ( std::complex<float>(std::exp(1.0)), std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0) *(1 << (value + 2))) ).real() < 0; }
Preferably with something more informative:That means "You must explicitly cast this".
ptr = (char*)str.c_str();
-PreludeCode:ptr = const_cast<char *> ( str.c_str() );
My best code is written with the delete key.
Bah, you can keep your const_cast, Long Live C!!
Honestly, though, does it really matter?
Code:#include <cmath> #include <complex> bool euler_flip(bool value) { return std::pow ( std::complex<float>(std::exp(1.0)), std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0) *(1 << (value + 2))) ).real() < 0; }
>Honestly, though, does it really matter?
Quick, what is this cast doing?
BLOCK *bblock = (BLOCK *)ablock;
Is it casting an object of a different type to BLOCK? Is it casting away const so that the assignment is legal? Is it trying to assign a pointer to a derived object to a pointer to the base object? Can you be sure of your answer? How about now?
BLOCK *bblock = const_cast<BLOCK *> ( ablock );
Amazing, instant self documenting code.
-Prelude
My best code is written with the delete key.
Argh. Never, never, never cast a const to non-const.
If string.c_str() returns a const char* this means you are NOT to modify whatever hides behind this pointer. So you should not cast it into something that can be modified. The problem is not your cast. The problem is your usage of char*. Why do you need this to be a char* ? I bet you could use a const char* instead.
If you post some code I can try to give an example. I have yet to see a situation where the const had to be removed for the program to work properly.
hth
-nv
She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."
When in doubt, read the FAQ.
Then ask a smart question.
uhh..how about just adding a comment?Originally posted by Prelude
>Honestly, though, does it really matter?
Quick, what is this cast doing?
BLOCK *bblock = (BLOCK *)ablock;
Is it casting an object of a different type to BLOCK? Is it casting away const so that the assignment is legal? Is it trying to assign a pointer to a derived object to a pointer to the base object? Can you be sure of your answer? How about now?
BLOCK *bblock = const_cast<BLOCK *> ( ablock );
Amazing, instant self documenting code.
-PreludeCode:ptr = (char*)str.c_str(); // const cast this variable i says!
How about doing it right in the first place ?
const char* cptr = NULL;
cptr = yourstringvar.c_str();
// No cast needed
hth
-nv
She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."
When in doubt, read the FAQ.
Then ask a smart question.
Well that is certainly true nvoigt, almost 100% of the time, you should respect a const. I really should have stated that in my first post.
Code:#include <cmath> #include <complex> bool euler_flip(bool value) { return std::pow ( std::complex<float>(std::exp(1.0)), std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0) *(1 << (value + 2))) ).real() < 0; }