Archived

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

johnnyBravo

whats wrong with using char * myString?

Recommended Posts

It''s just more error-prone and more of a hassle than using genuine std::strings. Please look into std::strings because they make your life so much easier.

char* has stupid disadvantages, like not being able to compare two with == (it compares addresses instead), having to use functions to assign one to another (strncpy), and having to watch out for buffer overruns. Generally, they''re just a pain.

Share this post


Link to post
Share on other sites
there''s no problem with it, but i suggest using it only when you need to allocate memory dynamicly. If you already know the length of the string before program execution, use char myString[10];
replacing 10 with the length you need.

hope that helps !
Matt

Share this post


Link to post
Share on other sites
If you are using C++ you should [usually] use std::string. Its a speed reason in terms of programming. If you are used to char* myString, then you may find you program using it quite well, but otherwise std::string affords a richer api for dealing with characters.

Share this post


Link to post
Share on other sites
Expanding on what lemurion said I would just let the compiler/linker figure out the size for me by doing:

char Stuff[] = "This is me!";

By doing it the above method it will figure out the amount of space and then add the \0 at the end of the string. Sometimes you might write, and I do this:

char word[3] = "Cat";

Now I missed the null terminator and then problems will ensue.

Share this post


Link to post
Share on other sites
std::string is just better all around and until profiling says otherwise its just the thing to use and well worth taking the short amount of time required to make the switch, trust me you'll never look back.
No fear of buffer overruns coz you havent saved enuff ram, built in find functions, perfect workinig with the STL, ability to add strings together to make one string using standard + notation.
My current project hasnt got a single char * in it, infact the only time i've needed to use a char * to read from the network i've replaced it with a vector instead

[edited by - _the_phantom_ on January 16, 2004 11:36:45 AM]

Share this post


Link to post
Share on other sites
quote:
Original post by codeguy
Expanding on what lemurion said I would just let the compiler/linker figure out the size for me by doing:

char Stuff[] = "This is me!";

By doing it the above method it will figure out the amount of space and then add the \0 at the end of the string. Sometimes you might write, and I do this:

char word[3] = "Cat";

Now I missed the null terminator and then problems will ensue.


Very strange, but shouldn''t it be:

char word[4] = "Cat";

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