The assignment C=R.c_str(); in the code listed below causes G++ to litter the complying with error:

error: invalid conversion from "const char*" to "char*" <-fpermissive>"

#include #include utilizing namespace std;int main(){ string R = "killme"; char *C = new char<100>; C=R.c_str(); coutWhy is this one error and how can I solve it?


The code has actually two problems. The main one, which reasons a translate into issue, is the assignment of c_str() result, i m sorry is const, to variable C, i beg your pardon is not const. The compiler tag this together an error, due to the fact that otherwise you could do this:

C=R.c_str();C<2> = "c";which would certainly write come a read-only area in memory, resulting in undefined behavior.

You are watching: Invalid conversion from ‘const char*’ to ‘char*’

You have the right to fix it in 2 ways:

Declare C a const, i.e. Const char *C = ..., orCopy the content into space that you have allocated.

The an initial approach is an easy - you perform this:

const char *C = R.c_str();The 2nd approach works favor this:

char *C = brand-new char;std::strcpy(C, R.c_str());The 2nd problem is a storage leak: your password assigns C a an outcome of new, however never deletes it. If you use strcpy approach, you should add

delete<> C;at the end of her program, as soon as you space done making use of variable C.

enhance this prize
edited Mar 30 "17 in ~ 8:21
reply Feb 13 "17 in ~ 3:07

Sergey KalinichenkoSergey Kalinichenko
687k7171 gold badges10251025 silver- badges14391439 bronze badges
add a comment |
I had actually the very same kind the problem. See if that works:

string R= "killme";char * cstr = new char ;strcpy (cstr, R.c_str());You have actually to include two libraries because that this:

enhance this answer
reply Oct 3 "18 at 15:55


1,00011 gold badge1414 silver- badges2525 bronze title
add a comment |
string::c_str() return a const char* not a char*. This is the factor of the error.

For much more information the the std::string::c_str() see the link:

Yes, both the point out mentioned above are additionally correct.

improve this price
answered Feb 13 "17 in ~ 4:35

3788 bronze badges
include a comment |

her Answer

Thanks for contributing an answer to stack Overflow!

Please be certain to answer the question. Carry out details and share her research!

But avoid

Asking because that help, clarification, or responding to various other answers.Making statements based upon opinion; back them up with references or an individual experience.

To discover more, watch our advice on writing good answers.

See more: How To Remove Poop Stains From Underwear, How To Remove Brown Stains In Briefs

Draft saved
Draft discarded

Sign increase or log in

authorize up making use of Google
sign up using Facebook
authorize up utilizing Email and Password

Post as a guest

email Required, but never shown

Post as a guest


Required, however never shown

short article Your price Discard

By clicking “Post your Answer”, you agree to our regards to service, privacy policy and also cookie policy

Not the answer you're spring for? Browse various other questions tagged c++ or ask your very own question.

The Overflow Blog
Featured ~ above Meta
Invalid conversion native 'char' come 'const char*' <-fpermissive>(idk why)
Arduino error: invalid conversion from 'const char*' come 'char*' <-fpermissive>
how to transform a std::string come const char* or char*
need assist debugging invalid conversion indigenous const char* come char* <-fpermissive>
weird Error, error: invalid conversion from ‘const char*’ to ‘char’ <-fpermissive>
ALL_OF invalid conversion from 'char' come 'const char*' <-fpermissive>
invalid conversion native 'const char*' to 'char*'
C++ invalid conversion native 'char' come 'const char*' <-fpermissive>|
C++ invalid conversion native 'const char*' to 'char*' Arduino Uno
warm Network inquiries an ext hot questions

question feed
i ordered it to RSS
concern feed To subscribe to this RSS feed, copy and paste this URL right into your RSS reader.

ridge Overflow
stack Exchange Network
site architecture / logo design © 2021 ridge Exchange Inc; user contributions license is granted under cc by-sa. Rev2021.11.23.40817

Stack Overflow works finest with JavaScript allowed

her privacy

By click “Accept all cookies”, friend agree stack Exchange have the right to store cookie on your maker and disclose details in accordance v our Cookie Policy.