Be a Supporter!

String vs char

  • 589 Views
  • 10 Replies
New Topic Respond to this Topic
Craige
Craige
  • Member since: Jul. 17, 2004
  • Offline.
Forum Stats
Member
Level 08
Blank Slate
String vs char 2005-06-22 18:51:20 Reply

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

thoughtpolice
thoughtpolice
  • Member since: Mar. 24, 2003
  • Offline.
Forum Stats
Member
Level 10
Blank Slate
Response to String vs char 2005-06-22 18:53:28 Reply

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
Response to String vs char 2005-06-22 18:54:25 Reply

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
Response to String vs char 2005-06-22 19:47:07 Reply

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
Response to String vs char 2005-06-22 22:16:48 Reply

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.

BBS Signature
thoughtpolice
thoughtpolice
  • Member since: Mar. 24, 2003
  • Offline.
Forum Stats
Member
Level 10
Blank Slate
Response to String vs char 2005-06-23 11:50:39 Reply

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
Response to String vs char 2005-06-23 12:07:38 Reply

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
Response to String vs char 2005-06-24 09:48:36 Reply

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
Response to String vs char 2005-06-24 11:36:09 Reply

char* + malloc = win

string = loose

Craige
Craige
  • Member since: Jul. 17, 2004
  • Offline.
Forum Stats
Member
Level 08
Blank Slate
Response to String vs char 2005-06-24 16:22:32 Reply

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.

Cahenn
Cahenn
  • Member since: Apr. 8, 2005
  • Offline.
Forum Stats
Member
Level 05
Blank Slate
Response to String vs char 2005-06-24 19:53:12 Reply

I like char arrays better, they are just supperior, what disadventage do they have ;)?