Jump to content
  • Advertisement

Archived

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

johnnyBravo

how to convert char to char * ?

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

ive got: char * string; char string2[10]; and i want to make string = string2. but when i try things like strcpy eg... this is orginal text: "hi there matie s" and after the conversion i get...: "hi there matie sÇ ↕" is there any proper way of converting it?

Share this post


Link to post
Share on other sites
Advertisement
try adding ''\0'' at the end of the string. NOTE: you can get the length of the string using strlen().

I hope this helps some.
-Boblin

Share this post


Link to post
Share on other sites
char * string = NULL;
char string2[10] = {0};

try that. it should work.

and that''s why in programming class they tell u to initialize your variables, especially your pointers/arrays.

then you don''t things like what you got

Share this post


Link to post
Share on other sites
Maybe strdup() - or something like it - will work out.
It allocates memory for a string and returns the pointer to it.

char *string = 0;
char src[] = "MyString";

string = strdup( src ); // not sure if this would compile, but it should work.

delete[] string; // delete the stringmemory
string = 0;

--
You''re Welcome,
Rick Wong
- sitting in his chair doing the most time-consuming thing..

Share this post


Link to post
Share on other sites
quote:
Original post by johnnyBravo
c++


Then ditch those unsafe, error prone char* and go the proper C++ way: std::string. Using char* for strings in C++ is just plain stupid (or ignorant, take your pick ).

std::string can and will hide all these problems from you, and prevent you making any silly mistakes.

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!