Jump to content
  • Advertisement
Sign in to follow this  
y2jsave

is this right?

This topic is 3584 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

Advertisement
It depends on what you mean by 'right'.
The syntax is right, and c_str does return char*, however what do you want to do with charStr?

Share this post


Link to post
Share on other sites
after
char* a = const_cast<char*>(b.c_str());

can we use a as an character array and access its elements by a (in c++)

Share this post


Link to post
Share on other sites
const char* c_str ( ) const;

c_str() returns a pointer to a constant buffer, so the code you've posted is not legal. Try this:

const char* charStr = str.c_str();

or (evil but possible)

char* charStr = const_cast<char*>(str.c_str());


--- Alex

Share this post


Link to post
Share on other sites
You can access the individual characters of a std::string with the [] operator. There's no need to retrieve a char pointer just for that reason.

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!