whats wrong with using char * myString?

Started by
8 comments, last by johnnyBravo 20 years, 3 months ago
ive been told not to use char * myString; . Why is this? Is there any speed or memory reasons for this? Thanks,
Advertisement
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.
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
Matt
Following on from what Alimonster said, I''d only use them for constant strings:

const char *hw = "Hello World!";
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.
ah i see.

Ive also been thinking of making own, just have a few things , eg compare, copy etc

As ive been creating my own little library of things eg math functions, dx stuff etc. But then again.....
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.
In the making Tread Wars - Tank Battle gamePowered by SKEnginehttp://www.highendgaming.com
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]
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..
quote:Original post by Pipo DeClown
Very strange, but shouldn''t it be:

char word[4] = "Cat";


nope, it should be std::string word("Cat");


This topic is closed to new replies.

Advertisement