'const char *' to 'char *'

Started by
8 comments, last by Emmanuel Deloget 18 years, 1 month ago
I'm trying to build a Log file but I keep getting this error:
Quote: error C2664: 'Log' : cannot convert parameter 1 from 'const char *' to 'char *' Conversion loses qualifiers
The problem is that my Log function takes as argument a 'char*' and I want to pass a std string. I'm trying this but it doesn't work: string Message; Log(Message.c_str()); Can someone point how this should be done?
Advertisement
Your Log function should take a const char*.
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.
Do you mean my method shold work or I should pass something else in my function?
If the later how do I convert the Message.c_str() to a const char*. I googled for
'const char *' to 'char *' but nothing helpful came up.
He means you should re-write your function so its parameter is const char*. And he is quite right.
Basically your Log() function should take a const char* as it doesn't modify the value of the variable that you're passing (or at least it shouldn't).
Every function that doesn't change a pointer should usually define the parameter as const, this way you'll know for sure that your pointer isn't being modified (in theory at least).
Basically just change
void Log( char* );
to
void Log( const char* );
Thanks! It worked just fine.
Quote:Original post by MHOOO
Basically your Log() function should take a const char* as it doesn't modify the value of the variable that you're passing (or at least it shouldn't).
Every function that doesn't change a pointer should usually define the parameter as const, this way you'll know for sure that your pointer isn't being modified (in theory at least).

I guess what you were trying to say is correct, but the wording is very wrong. It should read like this:

Basically your Log() function should take a const char* as it doesn't modify the value of the data referenced by the variable that you're passing (or at least it shouldn't).
Every function that doesn't change the data referenced by a pointer should usually define the parameter as pointer to const, this way you'll know for sure that the data referenced by your pointer isn't being modified (in theory at least).
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
say if the function did need to edit the data referenced by the pointer he could do something like this

std::string Message;char *tmp = strdup(Message.c_str());Log(tmp);//do something with the modified string datafree(tmp); 
_____________________#define ever (;;)for ever delete (void *) rand();
If you absolutely must do that conversion use a const_cast:
const char* const cc = "Hello!";char* c = const_cast<char*>(cc);

Of coorse this cast is generally not recommended and indicates poor programming.
----------------------------------------MagosX.com
Quote:Original post by Magos
If you absolutely must do that conversion use a const_cast:
*** Source Snippet Removed ***
Of coorse this cast is generally not recommended and indicates poor programming.


... or the need to adapt to some old pre-existing code [smile]

This topic is closed to new replies.

Advertisement