How to convert a char array to a string?

后端 未结 4 478
耶瑟儿~
耶瑟儿~ 2020-11-27 09:49

Converting a C++ string to a char array is pretty straightorward using the c_str function of string and then doing strcpy. However, ho

4条回答
  •  悲哀的现实
    2020-11-27 10:25

    #include 
    #include 
    #include 
    #include 
    
    using namespace std;
    
    int main ()
    {
      char *tmp = (char *)malloc(128);
      int n=sprintf(tmp, "Hello from Chile.");
    
      string tmp_str = tmp;
    
    
      cout << *tmp << " : is a char array beginning with " <

    OUT:

    H : is a char array beginning with 17 chars long
    
    Hello from Chile. :is a string with 17 chars long
    

提交回复
热议问题