-3

I'm trying to figure out how I could convert a string into a char, I keep seeing posts online to use strcpy but I can't figure out how to use it. Can someone show an example?

Darunia
  • 21
  • 4
  • A string is a sequence of multiple chars. How do you expect to put it into a single char? It's not clear what you are trying to achieve. – Igor Tandetnik May 31 '20 at 01:37
  • 1
    If you'd like to get a C-style `const char *` from your `std::string`, you can do it like this: `const char * myPtr = myString.c_str();` (note that the pointed-to-characters are still owned by the `std::string` object and will likely become invalidated if you modify the `std::string`, so make a copy of them if you need to) – Jeremy Friesner May 31 '20 at 01:40

1 Answers1

0

How about?:

std::string foo = "Your string";
const char* as_char = foo.c_str();
Amal K
  • 4,359
  • 2
  • 22
  • 44
  • The pointer returned may be rendered invalid by further calls to the object member functions, so make sure you make a copy of it if you need to. – paxdiablo May 31 '20 at 01:46