-4

I'm trying to find how to convert a char array into a std::string, but I can't find anything to do this.

If it is impossible, could you explain me how can I read an std::string but by scanf() and gets() I can't.

Best regards.

anrequete
  • 85
  • 2
  • 10

1 Answers1

0

The simplest way of making a string from a character array is using a constructor that takes a pointer and the length, like this:

char charArray[] = {'w', 'x', 'y', 'z'};
std:string s(&charArray[1], 2); // s is "xy"
//                ^         ^
//                |         |
//    Pointer to the        |
//    starting position   Length
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523