Home c++ How to convert string to char?

How to convert string to char?

Author

Date

Category

There is an arbitrary string of no more than 25 characters. For example “wo1fram”
How to convert it to char array [255]?
So that later you can work with char as with a full-fledged array of characters, terminated by a null character.


Answer 1, authority 100%

There are two solutions, you see.

One thing – if you just need to read this line, or there, change a couple of characters in it – but do not change its size (so all sorts of strcpy are canceled) – then you can use the functions c_str () and data () . I highly recommend that you carefully read the descriptions, and most importantly, the restrictions imposed by these functions.

And another thing – if you need to work with it as a C-style string with all the possibilities – then just copy it into an array, like

char buf [255];
strcpy (buf, s.c_str ());

or

char * buf = strdup (s);

Something like this.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions