31

I've got a const char * returned from a processing function, and I'd like to convert/assign it to an instance of std::string for further manipulation. This seems like it should be straight-forward, but I've not been able to find any documentation showing how it should be done. Obviously, I'm missing something. Insights appreciated.

Justin Fletcher
  • 2,319
  • 2
  • 17
  • 32
  • 8
    It's one of the constructor options, ``std::string(const char* cstr)`` – aruisdante Jun 09 '14 at 19:58
  • `std::string s = f();` – Alan Stokes Jun 09 '14 at 19:59
  • This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting. – πάντα ῥεῖ Jun 09 '14 at 20:05
  • Wow, -5. I didn't think it was a terrible question... Oh well. – Justin Fletcher Jun 09 '14 at 21:06

5 Answers5

62

std::stringhas a constructor fromconst char *.This means that it is legal to write:

const char* str="hello";
std::string s = str;
Ivaylo Strandjev
  • 69,226
  • 18
  • 123
  • 176
22

Try

 const char * s = "hello";
 std::string str(s);

will do the trick.

Ed Heal
  • 59,252
  • 17
  • 87
  • 127
  • 1
    I find it amusing that we chose the same example string and almost the same(just swapped) variable names :-) – Ivaylo Strandjev Jun 09 '14 at 20:02
  • 1
    Just make sure that your char * isn't NULL, or else the behavior is undefined. – Trevor Hickey Nov 06 '15 at 20:48
  • How is it going to be null? – Ed Heal Nov 06 '15 at 20:50
  • 3
    @EdHeal It's not in your example, but a char* in general could be. I'm leaving a disclaimer for others. For example, I'm using the getenv() function. If the environment variable doesn't exist it will return a null. A check would be needed before constructing a string of that function's return value. This is relevant since OP says they "got a const char * returned from a processing function". – Trevor Hickey Nov 06 '15 at 21:02
4

You've got a lot of options:

const char* dosth() { return "hey"; }

string s1 = dosth();
string s2 (dosth());
string s3 {dosth()};
auto   s4 = (string)dosth();

Live Demo, Documentation

Note that s3 and s4 are C++11 features, if you should still work with an old or non-compliant compiler you would have to work with one of the other options.

Appleshell
  • 7,088
  • 6
  • 47
  • 96
3

std::string has a constructor that converts const char* implicitly. In most cases, you need to do nothing. Just pass a const char* where a std::string is accepted and it will work.

Fred Larson
  • 60,987
  • 18
  • 112
  • 174
3

There are three possibilities. You can use a constructor, an assignment operator or member function assign (if do not take into account member function insert though it is also can be used:) )`

For example

#include <iostream>
#include <string>

const char * f() { return "Hello Fletch"; }

int main()
{
   std::string s1 = f();

   std::string s2;
   s2 = f();

   std::string s3;
   s3.assign( f() );

   std::cout << s1 << std::endl;
   std::cout << s2 << std::endl;
   std::cout << s3 << std::endl;
}
Trevor Hickey
  • 36,288
  • 32
  • 162
  • 271
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335