Click here to Skip to main content
15,851,387 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In fun1() I am getting a string and I need to pass this string data to a function fun2() which takes argument char *

function2 prototype is as below

void fun2(char *p);
I am calling fun2 from fun1 as below

void fun1()
fun2((char *)str.c_str());
Is there any chance from the conversion from const char * to char * does make any potential issues
nv3 7-Mar-15 12:15pm    
Well, what did you think the "const" attribute is good for? The const says: "Don't touch this!" and the compiler gives you a warning if you try nonetheless.

If the author of fun2 just forgot to specify the argument as "const", your attempt might go through without consequences. But if fun2 really needs a (modifiable) "char*", you should take utmost care. Solution1 tells you what to do in this case.
CPallini 7-Mar-15 15:11pm    
(virtual) 5.
nv3 8-Mar-15 4:59am    
Than you, Carlo.

A const char* can't be converted to a char* because it would open up the possiblity of changing the value of constants: in this case potentially changing (say) a user prompt from "hello user!" to "HeLlO UsEr!" and making the fixed prompt change half way through the program.
If you need to pass a const char* to a function taking a char*, you need to allocate some char memory, and copy the constant string into it - then you can pass the non-const copy to your function.
Share this answer
Andreas Gieriet 7-Mar-15 14:58pm    
My 5!
CPallini 7-Mar-15 15:11pm    
krish0969 8-Mar-15 7:46am    
Thanks for the response.For me if the value is changed in the fun2 its not really impact my functionality.I just want to know is there any impact like memory corruption
OriginalGriff 8-Mar-15 8:03am    
No, I think you misunderstand.
When I said "can't be converted" it's because the system won't let you - as soon as you try to change anything your application will crash because you are trying to write to a read only memory segment!
Think of it like this:
If you have a function:

void myFunc(char * str)
*str = '\0';

what happens when you call it:


It's like trying to do this:

void Add(int* pi)
*pi +=10;

And calling it like this:


Constants are called that for a reason! :laugh:
So the system is set up to not allow you to remove the "const" attribute.
You can get a modifiable C string from a C++ string by simply taking the address of the first character. Use

void fun1()

(the only catch is if fun2 happens to modify the terminating null character of its C string argument, the null appended by the C++ string in this situation is not modifiable)
Share this answer
Andreas Gieriet 7-Mar-15 14:57pm    
This does not answer the question.
1) it is about the consequences of casting away constness
2) if str is const, each element of the str is const again.
CubbiMew 7-Mar-15 14:59pm    
I am not answering the question title, I am answering the question content.
Andreas Gieriet 7-Mar-15 15:06pm    
The question is: "[...] Is there any chance from the conversion from const char * to char * does make any potential issues [...]".

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900