Jump to content
  • Advertisement
Sign in to follow this  
Dan Caranfil

'const char *' to 'char *'

This topic is 4549 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
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* );

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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 data
free(tmp);


Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!