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.
Asked
Active
Viewed 1.3e+01k times
27
Justin Fletcher
- 2,239
- 2
- 16
- 31
-
7It'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
-
1+1: Yeah -9 for this question is kind of ridiculous. The other question is slightly different because it has a length requirement which may not be relevant to many people. – User Aug 15 '14 at 22:39
5 Answers
59
std::stringhas a constructor fromconst char *.This means that it is legal to write:
const char* str="hello";
std::string s = str;
Ivaylo Strandjev
- 66,530
- 15
- 117
- 170
-
6Just make sure that your char * isn't NULL, or else the behavior is undefined. – Trevor Hickey Nov 06 '15 at 20:18
20
Try
const char * s = "hello";
std::string str(s);
will do the trick.
Ed Heal
- 57,599
- 16
- 82
- 120
-
1I 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
-
1Just make sure that your char * isn't NULL, or else the behavior is undefined. – Trevor Hickey Nov 06 '15 at 20:48
-
-
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();
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
- 6,778
- 5
- 46
- 94
-
-
1Just make sure that your char * isn't NULL, or else the behavior is undefined. – Trevor Hickey Nov 06 '15 at 20:48
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
- 34,154
- 27
- 144
- 256
Vlad from Moscow
- 265,791
- 20
- 170
- 303
-
Just make sure that your char * isn't NULL, or else the behavior is undefined. – Trevor Hickey Nov 06 '15 at 20:48
2
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
- 58,972
- 15
- 110
- 164
-
Just make sure that your char * isn't NULL, or else the behavior is undefined. – Trevor Hickey Nov 06 '15 at 20:48