Sign in to follow this  
Bastian Kuolt

Converting std::string into a C- string

Recommended Posts

std::string(myCString)

or into a variable:

std::string myCplusplusString = myCString;

If you have a function that takes a const std::string reference then you can pass a C string to it:

void foo( const std::string &str ) {}

void bar() { foo("Hello, world."); }

Share this post


Link to post
Share on other sites
Quote:
Original post by rip-off
If you have a function that takes a const std::string reference then you can pass a C string to it:


I guess it's worth mentioning that the argument being const or referenced are not required. Passing a C string to an argument that is expecting a std::string works by passing the C string to the std::string constructor that accepts a single C string (the proper term as i think its called is implicit type conversion).

Also, if your passing a C string to an argument thats expecting a reference to a std::string, your compiler usually will first create a temporary unnamed std::string object from the given C string, then pass its reference as the argument. This would be ever so slightly less efficient then passing a C string as an argument thats expecting a std::string, due to the non-creation of a temporary object, and the avoidance of passing a seemingly redundant reference.

Share this post


Link to post
Share on other sites
Quote:
Original post by Thetan
Quote:
Original post by rip-off
If you have a function that takes a const std::string reference then you can pass a C string to it:


I guess it's worth mentioning that the argument being const or referenced are not required. Passing a C string to an argument that is expecting a std::string works by passing the C string to the std::string constructor that accepts a single C string (the proper term as i think its called is implicit type conversion).


Its idiomatic C++ to pass types with non-trivial construction/copy operations by const reference to minimise the impact of such operations.

This is why I used a const reference in my post.

Quote:

Also, if your passing a C string to an argument that is expecting a reference to a std::string, your compiler usually will first create a temporary unnamed std::string object from the given C string, then pass its reference as the argument. This would be ever so slightly less efficient then passing a C string as an argument thats expecting a std::string, due to the non-creation of a temporary object, and the avoidance of passing a seemingly redundant reference.


Passing a C string to a function taking a std::string by value involves the creation of a temporary too. But it will also involve a copy operation of every std::string passed to the function, even where this is not required.

I fail to see your argument in this paragraph. This test program I wrote illustrates:


struct Example {
Example( const char * ) { std::cout << "Example(const char *)\n"; }
Example( const Example & ) { std::cout << "Example( const Example & )\n"; }
~Example(){ std::cout << "~Example()\n"; }
};

void foo( Example )
{
}

void bar( const Example & ){}

int main()
{
foo("42");
bar("1984");
Example e("Hello, World");
foo(e);
bar(e);
}




Output:

Example(const char *)
~Example()
Example(const char *)
~Example()
Example(const char *)
Example( const Example & )
~Example()


As you can see the output is the same, except where we pass a already constructed Example object to the function foo by value, where an additional copy operation occurs.

Share this post


Link to post
Share on other sites
Quote:
Original post by Bastian Kuolt
How can I convert a std::string into a C string?
Use the .c_str() member function

Share this post


Link to post
Share on other sites
For future reference don't edit your initial post, just reply to your topic. When you edit your post it doesn't show up on the "active topics" as changed, so many would miss the change.

Share this post


Link to post
Share on other sites

string s = "hello";
char c_string [MAX_LEN + 1]; // 1 for NULL char

if (s.size() <= MAX_LEN)
strcpy (c_string,s.c_str());
else
something_else();

Share this post


Link to post
Share on other sites
For modifiable c strings:


std::string foo = "whatever";

char* planA = strdup(foo.c_str()); // free with free(planA);

char* planB = new char[foo.size() + 1];
memcpy(planB, foo.c_str(), foo.size() + 1); // free with delete [] planB




Personally I rather like planA, but I'm a bad person. [grin]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this