Jump to content
  • Advertisement

Archived

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

johnnyBravo

how to convert string to char [] ??

This topic is 5224 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
TheString.c_str() returns "const char*". The const means it''s supposed to be read-only.

~CGameProgrammer( );

Screenshots of your games or desktop captures -- Upload up to four 1600x1200 screenshots of your projects, registration optional. View all existing ones in the archives..

Share this post


Link to post
Share on other sites
Guest Anonymous Poster

char buf[100]; // Warning, may be too small
std::string str = "gnaaa";

strcpy( &buf[0], str.c_str() );

Share this post


Link to post
Share on other sites
quote:

char buf[ str.size() ]; // Warning, may be too small

std::string str = "gnaaa";
strcpy( &buf[0], str.c_str() );




Since the string would already be created (otherwise, why care about convert it?), isn''t this a better solution?

std::string str = "gnaaa"; //assumed

char buf[ str.size() ]; // just the right size
strcpy( &buf[0], str.c_str() );

Share this post


Link to post
Share on other sites
quote:
Original post by debaere
quote:

char buf[ str.size() ]; // Warning, may be too small

std::string str = "gnaaa";
strcpy( &buf[0], str.c_str() );




Since the string would already be created (otherwise, why care about convert it?), isn't this a better solution?

std::string str = "gnaaa"; //assumed

char buf[ str.size() ]; // just the right size
strcpy( &buf[0], str.c_str() );


good idea, but c++ doesnt allow you to put values like that in, as i get the error
error C2057: expected constant expression
error C2466: cannot allocate an array of constant size 0
error C2133: 'buf' : unknown size

edit:

i guess i could use pointers, but i really want to leave that to last resort...or vectors maybe

[edited by - johnnyBravo on February 28, 2004 8:58:35 AM]

Share this post


Link to post
Share on other sites
quote:
Original post by debaere
char buf[ str.size() ]; // just the right size



You forgot the terminating NULL. You need str.size() + 1.

Just use strdup() and free() the memory later.


std::string s = "hello";
char *pc = strdup(s.c_str());
// Do something

free(pc);





Kami no Itte ga ore ni zettai naru!

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by johnnyBravo
instead of having to free up the char *,

i just put strdup(filename.c_str()) straight into the function,


Wait! What do you mean by not having to free up but still using strdup()? That''s a memory leak!

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.

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

Sign me up!