Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

AABBFFLLUUGG

Help using std::string !

This topic is 5302 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 really want to use std::string in place of messing around with loads of char* this and char[x] that, but I dont know how to use strings when functions require char* ! for example, the recv function in winsock requires a char* parameter for the data buffer, but when i try and give it a string, it says it cannot convert! how (in this example) would i enter data into the string using the recv function? bye :D

Share this post


Link to post
Share on other sites
Advertisement
I think what you need is c_str(). So to get the C string of a string named MyStr: MyStr.c_str()
I don''t have my links handy, but there should be some online docs somewhere with more details of c_str().

Tadd
- WarbleWare

Share this post


Link to post
Share on other sites
string as a method called c_str() for this:

std::string s;

void foo( char* );

foo( s.c_str() );

Share this post


Link to post
Share on other sites
If the function only needs a const char* , then you can call the .c_str() method on your std::string object, and it will return a const char* . However, if the function requires a non-const char* , then you're stuck creating a temorary character array buffer to pass in to the function. Then, after the function call, you set your std::string object equal to the character array buffer. This will copy all the characters, and then you can do away with your buffer. A pain, yes, but necessary when working with certain functions. If you write your own functions that need to work with strings, make sure that you do use const std::string& and non-const std::string& . It's so much nicer. And it's not really any slower, assuming you remember to make them references.

[Edit - Wow, I thought I typed this pretty quickly this time, but three posts were made while I was typing, two from the same person. I'm slow... ]

[edited by - Agony on April 15, 2004 1:16:28 PM]

Share this post


Link to post
Share on other sites
thanks a lot, i think i will have to do the temporary char solution then, as afaik you cannot write to c_str() (although i could be very wrong)

Share this post


Link to post
Share on other sites
quote:
Original post by AABBFFLLUUGG
thanks a lot, i think i will have to do the temporary char solution then, as afaik you cannot write to c_str() (although i could be very wrong)

Nope, it''s constant.

Well, you could write to it if you really wanted to, but you''re probably better off not doing so.

Share this post


Link to post
Share on other sites
You can use const_cast<> to cast away c_str()''s const-ness if and only if the function you pass it to is polite about not modifying the string. A better solution would be to fix the function declaration in that case, but there are situations where you can''t.

Share this post


Link to post
Share on other sites

  • 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!