Sign in to follow this  
Dan Caranfil

'const char *' to 'char *'

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
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this