Is there a way to convert from const char * to char * without casting away the const? I was warned that it could be dangerous and cause crashes (which I would agree with, since when I ran my program with const_cast, it caused a segmentation fault). Alternatively, is there a way to convert from string to char * (not convert from string to const char *, as c_str() does)? Any suggestions would be greatly appreciated.
- Invalid Conversion From Const Char To Char Dev C Download
- Cast Const Char To Char
- C Char To Const Char
- 3 Contributors
- forum 4 Replies
- 14,935 Views
- 15 Hours Discussion Span
- commentLatest Postby Daria ShmariaLatest Post
Invalid Conversion From Const Char To Char Dev C Download
>Is there a way to convert from const char *
>to char * without casting away the const?
Short answer: No.
Everything in memory has an address. Arrays are in consecutive memory addresses. A pointer is a variable that contains an address. The pointer can be used to fetch the data at the address. Mar 04, 2010 Help fixing C invalid conversion from const char. to char User Name: Remember Me? Sausage fattener vst free download pc windows 10. Password: Programming This forum is for all programming questions. The question does not have to be directly related to Linux and any language is fair game. Notices: Welcome to LinuxQuestions.org, a friendly and active Linux Community.
Cast Const Char To Char
>I was warned that it could be dangerous and cause crashes
It's const for a reason. For example, when you point to a string literal, the pointer should be declared as 'const char *' because string literals are stored in read-only memory. Casting away the const won't change the read-only nature of the data itself, and trying to modify it will probably cause a crash.
C Char To Const Char
You should only cast away const if you're so sure that the chamber is empty, you'd be willing to point the gun at your face and pull the trigger without any hesitation. In all other cases, work around the const. Make a copy, for example: