Jump to content
  • Advertisement
Sign in to follow this  
baker

how to make a character pointer point to a string?

This topic is 4581 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hi, i need to pass a string to a function that only accepts char pointer. would something like this work? string s("hello, i eat bbq"); char *myCharPtr; s.copy(myCharPtr, s.size()); sendData(myCharPtr);

Share this post


Link to post
Share on other sites
Advertisement
Use std::string::c_str() to convert a string to a c-style string. Like -

std::string foo = "hiya";
char* cstring = foo.c_str();


Or you can just pass it directly to the function.

myFunc( str.c_str() );

Share this post


Link to post
Share on other sites
Yup, the c_str() member function is what you're looking for. Be careful about keeping this pointer around though. I don't know what will happen if you try to modify the string after called c_str() then read from the c string. Or if it copies to a seperate buffer, what happens if you call c_str(), then modify, and call c_str() again.

Share this post


Link to post
Share on other sites
this is the error i got when using c_str

sendData' : cannot convert parameter 1 from 'const char *' to 'class SomeData *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

Share this post


Link to post
Share on other sites
Quote:
I don't know what will happen if you try to modify the string after called c_str() then read from the c string. Or if it copies to a seperate buffer, what happens if you call c_str(), then modify, and call c_str() again.

The char* returned from c_str() (or data for that matter) will reflect changes made to the string. That data is owned and maintained by the string though, so don't attempt to modify/free the data witht he pointer returned (will result in undefined behavior). But you can read from it fine, and expect it to stay "correct".

Share this post


Link to post
Share on other sites
Quote:
Original post by Mushu
The char* returned from c_str() (or data for that matter) will reflect changes made to the string. That data is owned and maintained by the string though, so don't attempt to modify/free the data witht he pointer returned (will result in undefined behavior). But you can read from it fine, and expect it to stay "correct".
Since it's a const pointer, you can't modify the data without using a cast to begin with, which should be a tip off that something is not right.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!