String vs char
- Craige
-
Craige
- Member since: Jul. 17, 2004
- Offline.
-
- Forum Stats
- Member
- Level 08
- Blank Slate
This sorta relates to the other thread. but I wanted to keep that one clean for explaining the other problem. So here I ask, which is better in most situations, string's or char array's?
C++ By the way
// MustyWindows - Jump Through The Windows
// AmpFusion - Where Underground Becomes Mainstream
Neo Enterprise Technologies Coming soon.
- thoughtpolice
-
thoughtpolice
- Member since: Mar. 24, 2003
- Offline.
-
- Forum Stats
- Member
- Level 10
- Blank Slate
string type. It was made for C++ being a class and all, while C is procedural.
Kind of obvious.
omg.
Playstation Network tag: muffin-noodle
the empty set
- Cinjection
-
Cinjection
- Member since: Apr. 6, 2004
- Offline.
-
- Forum Stats
- Member
- Level 18
- Blank Slate
String in most cases, is better. It dynamicly allocates memory so it protects against stack overflows from what i understand(Sinner confirm?). I thin kyou should use chars if you need a single character as a responce. Such as 'y' or 'n'. Otherwise use String.
- thoughtpolice
-
thoughtpolice
- Member since: Mar. 24, 2003
- Offline.
-
- Forum Stats
- Member
- Level 10
- Blank Slate
This question was answered before it was asked.
Obviously, C++ being an Object-Oriented language, using a class such as the string type is a MUCH better choice than using ugly-ass character arrays. It dynamically allocates memory and you can do integer-like comparison with them, which can result in this:
if(stringBlah == "yay")
Rather than this
if(stcmp(charAryBlah,"yay")==0)
Same goes for characters in C++ (although integer comparison works on single cars in C as well)
An object-oriented language should use Object-Oriented class and such instead of anything else, that's the framework they're built upon. It's ment to be, like an unwritten rule.
omg.
Playstation Network tag: muffin-noodle
the empty set
- LordAba
-
LordAba
- Member since: Nov. 2, 2000
- Offline.
-
- Forum Stats
- Member
- Level 10
- Blank Slate
At 6/22/05 07:47 PM, Sinnernaut wrote: An object-oriented language should use Object-Oriented class and such instead of anything else, that's the framework they're built upon. It's ment to be, like an unwritten rule.
Bah, I say BAH! In most cases you would be correct, but given that C++ is C with classes, it's very dynamic in the way you can use(or not use) them.
Anyways, a string and a char array are the same thing pretty much. It's just that the string class in the STL supply a nice set of wrappers around it.
To see their real simularity you'd have to use assembly.
What may man within him hide, though angel on the outward side.
- thoughtpolice
-
thoughtpolice
- Member since: Mar. 24, 2003
- Offline.
-
- Forum Stats
- Member
- Level 10
- Blank Slate
At 6/23/05 11:44 AM, JesusCyborg wrote: To be honest, I hate everything about STL so I'd go with char*
Oh JC... Only STL?
omg.
Playstation Network tag: muffin-noodle
the empty set
- PONGpaddle
-
PONGpaddle
- Member since: Sep. 23, 2003
- Offline.
-
- Forum Stats
- Member
- Level 13
- Blank Slate
Use the string class in C++ because it takes care of all the little details for you, and has some rather cool functions in it that you previously needed stdlib.h (aka cstdlib) for, or needed to write yourself.
- CronoMan
-
CronoMan
- Member since: Jul. 19, 2004
- Offline.
-
- Forum Stats
- Member
- Level 06
- Blank Slate
I don't trust strings in C++
I use char * instead.
It's not like char* is a big problem or anything.
"no sound in ass"
- WhiteBlackPerson
-
WhiteBlackPerson
- Member since: Aug. 4, 2001
- Offline.
-
- Forum Stats
- Member
- Level 04
- Blank Slate
- Craige
-
Craige
- Member since: Jul. 17, 2004
- Offline.
-
- Forum Stats
- Member
- Level 08
- Blank Slate
Well it looks like i have some people saying string and some people saying char array's. However I think I will stick with char array's. My book uses them, and it's a little somthing new. It may take a little bit more to check things and make sure input will not currupt the program, but oh well. If i feel its too much work, i can always switch to string. Just one more header to include.
// MustyWindows - Jump Through The Windows
// AmpFusion - Where Underground Becomes Mainstream
Neo Enterprise Technologies Coming soon.
- Cahenn
-
Cahenn
- Member since: Apr. 8, 2005
- Offline.
-
- Forum Stats
- Member
- Level 05
- Blank Slate
I like char arrays better, they are just supperior, what disadventage do they have ;)?


