CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 24

Thread: char vs string

  1. #1
    Join Date
    Jul 2007
    Location
    Illinois
    Posts
    517

    char vs string

    Ok, my instructor for my CS II class is insisting we use use the char datatype versus the string datatype. He gave us a few reasons (very loosely knit) as to why the char datatype is better.

    My query is, which one IS better? I know that I have always had alot of trouble with the char datatype just because it can have a subscript, and it can do without a subscript. Also, there dont seem to be predefined functions to work with the datatype char so it is just generally a pain in the butt even if you produce your own functions.

    Now string on the other hand has a slew of functions already built to work with it such as .length(), AND its just all around easier to work with.

    What do you guys think?
    R.I.P. 3.5" Floppy Drives
    "I know not with what weapons World War III will be fought, but World War IV will be fought with sticks and stones." - Albert Einstein

  2. #2
    Join Date
    Dec 2006
    Location
    England
    Posts
    30

    Re: char vs string

    I agree that the string class, if it can be called that, is easier to use. However when using old APIs it's easier to use char arrays instead of strings because many functions have to be passed char pointers. It depends what you're working with.
    - Matt J Thomas

  3. #3
    Join Date
    Aug 2005
    Location
    Netherlands, The
    Posts
    2,184

    Re: char vs string

    string is better than using plain chars yourself.
    because u dont have to worry about allocations and such.

    but string is still not a decent class (most of the STL classes arent in my opinion, but thats why they are called containers)

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: char vs string

    Quote Originally Posted by RaleTheBlade
    Ok, my instructor for my CS II class is insisting we use use the char datatype versus the string datatype. He gave us a few reasons (very loosely knit) as to why the char datatype is better.
    Have your instructor look at this document, written by the inventor of C++, Bjarne Stroustrup. He disagrees with your teacher, and gives reasons why.

    http://www.research.att.com/~bs/new_learning.pdf

    Also, char is only a single character. That is not a string. If your teacher means char arrays, then there are security issues that arise when handling char arrays, such as buffer overruns. Some C++ shops disallow any use of char arrays and functions such as strcpy(), strcat(), etc. because of these reasons.

    Regards,

    Paul McKenzie

  5. #5
    Join Date
    Apr 1999
    Posts
    27,449

    Re: char vs string

    Quote Originally Posted by Matt J Thomas
    I agree that the string class, if it can be called that, is easier to use. However when using old APIs it's easier to use char arrays instead of strings because many functions have to be passed char pointers. It depends what you're working with.
    You can still use strings. You have the c_str() member function, and if the string is writeable, you have std::vector<char>.

    In this day and age of C++, rarely, if ever, do you need to use the new[]/delete[] way of creating a string or string buffer.

    Regards,

    Paul McKenzie

  6. #6
    Join Date
    Nov 2006
    Posts
    1,611

    Re: char vs string

    I find it amusing that a CS instructor would profess character arrays to be superior to string classes.

    I would genuinely enjoy reading his reasoning if you would be so kind as to post it. In true Platonic spirit I would want to hear of a salient point that might be illuminating, even if I must admit I doubt there can be any.

    There are a number of good string classes, and the STL string class may not be the best of them. I've used a portable, 'almost' drop in replacement of the MFC CString class (also loosely based upon the STL basic_string) for portable development with good effect. I suppose one of the reasonable arguments against string classes (though not compelling IMO) is the fact that there are several string classes. I suppose these days there may be agreement that the STL string class is the standard, but obviously developers using various frameworks have other relationships to consider (MFC's use of CString for example).

    The const char array (and char array) is generally widely applicable, and perhaps that's what the professor is thinking.

    Containment is more compelling as a reason, I think - favoring string classes.
    If my post was interesting or helpful, perhaps you would consider clicking the 'rate this post' to let me know (middle icon of the group in the upper right of the post).

  7. #7
    Join Date
    Jul 2007
    Location
    Illinois
    Posts
    517

    Re: char vs string

    Quote Originally Posted by Paul McKenzie
    Have your instructor look at this document, written by the inventor of C++, Bjarne Stroustrup. He disagrees with your teacher, and gives reasons why.

    http://www.research.att.com/~bs/new_learning.pdf
    ^^^^^^^^^^^^

    Impressive Paul, thanks! I read that and got a better idea of just why strings are generally better.

    Im not really sure why he thinks char's and char arrays are better. Personally I hate them, lol. And to answer your question JVene, I believe he said something about a string variable not having a null terminator at the end. And also something about the spaces between collective words in a string causing search issues or something. My mind kind of shut off as I quickly realized that this semester may very well be a proverbial hell if he pushes the char datatype onto us and discards anything string.

    OFF TOPIC EDIT:
    On a side note, I just put honey in my coffee. Its pretty good actually
    Last edited by RaleTheBlade; August 25th, 2007 at 01:39 PM.
    R.I.P. 3.5" Floppy Drives
    "I know not with what weapons World War III will be fought, but World War IV will be fought with sticks and stones." - Albert Einstein

  8. #8
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: char vs string

    I quickly realized that this semester may very well be a proverbial hell if he pushes the char datatype onto us and discards anything string
    If he does not discard classes, you could write your own string class and use that.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  9. #9
    Join Date
    Jul 2007
    Location
    Illinois
    Posts
    517

    Re: char vs string

    Quote Originally Posted by laserlight
    If he does not discard classes, you could write your own string class and use that.
    LOL, I was already thinking about that. I was actually working on a class with a function to compare char arrays to strings but then I was like "Why dont I just demand that I use string? Its me that paying for the education!"
    And besides, if he demands that we use char... he's not really teaching us to use things the proper way, is he? Strings from what Ive always known in the past, are just better on many different levels.
    R.I.P. 3.5" Floppy Drives
    "I know not with what weapons World War III will be fought, but World War IV will be fought with sticks and stones." - Albert Einstein

  10. #10
    Join Date
    Nov 2006
    Posts
    1,611

    Re: char vs string

    They are better on many levels.

    The 'zero termination' notion is a red herring.

    I have no idea what spaces have to do with it - sounds odd.
    If my post was interesting or helpful, perhaps you would consider clicking the 'rate this post' to let me know (middle icon of the group in the upper right of the post).

  11. #11
    Join Date
    Aug 2005
    Location
    Netherlands, The
    Posts
    2,184

    Re: char vs string

    the only reason i see fit to use dynamic char arrays and handling them yourself is to learn why string classes are better and how to make them yourself and how to learn how memory works. but if he is using this method to teach you C++ he is seriously wrong in doing so

  12. #12
    Join Date
    Nov 2006
    Posts
    1,611

    Re: char vs string

    Bravo Mitsukai!

    Teacher: "Go ahead, put your hand on the hot stove. Tell me what you discover!"

    A lesson more valuable than simply being told!
    If my post was interesting or helpful, perhaps you would consider clicking the 'rate this post' to let me know (middle icon of the group in the upper right of the post).

  13. #13
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: char vs string

    Teacher: "Go ahead, put your hand on the hot stove. Tell me what you discover!"
    Student: "Yum! Tasty! Where did my hand go?"
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  14. #14

    Re: char vs string

    Using a string is a much better choice than a char array. UNLESS, the only thing you are doing is storing text data, with possible appends.

    If you are inserting, etc... Then a string type is better.

  15. #15
    Join Date
    Jul 2007
    Location
    Illinois
    Posts
    517

    Re: char vs string

    I knew string was better, I will continue to use it until he tells me to stop (or gives me an F) and then I will bring up the points you guys have told me. Thanks!
    R.I.P. 3.5" Floppy Drives
    "I know not with what weapons World War III will be fought, but World War IV will be fought with sticks and stones." - Albert Einstein

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured