strings or arrays of characters?

This is a discussion on strings or arrays of characters? within the C++ Programming forums, part of the General Programming Boards category; Bleh. I'm not really sure what the difference is between a 'string' and a 'c string' and I was having ...

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    25

    strings or arrays of characters?

    Bleh. I'm not really sure what the difference is between a 'string' and a 'c string' and I was having troubles from before that unfortunately haven't been resolved. (The previous thread is at Console-based file i/o troubles)

    I was wondering if it's more practical to use the old C strings than it is to use a class of strings.

    I -think- a C string is really just a pointer to the first element in an array of characters, whereas a C++ string is a class.

    If that's the case, wouldn't it be better to use the class string? I know that can really only be answered in context, but take for example the program I was writing that used the class string for file i/o.

    One particular reply made it seem that C++ strings weren't used as often as C strings. If someone could point out the pros and cons of each, or direct me to some sort of site that can help me with this, that'd be great. =)

    I'm a beginner, and if you look at the code (attatched to the aforementioned thread) you can probably tell. =P The program works for the most part; the only thing wrong with it is saving a list. Sometimes the lists save properly, but in other cases it wil duplicate the list instead of writing over it.

    The way it's supposed to save is look through the lists that are already saved, and if it's a new list it just adds it to the file. This works fine. If it's an already existing list, it reads the old lists into a representation of the new lists, and excludes the old list.

    At this point the representation (done with vectors) contains the rest of the lists, and not the old list. Then, it adds the list being saved to the end, just as if it were a new list.

    It determines which list to exclude by comparing the names of the lists, and if they match, it skips over it while reading into the representation (rather, reads it, but discards it).

    For some reason that only works once! If the list being over written is any list but the first list, this won't work.

    Any help is welcome. O.o
    Thanks in advance.

  2. #2
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    More accurately a C-string is the array of chars, and the pointer is simply a tool to aid in using that string. (edit: a better definition just came to mind - it generally refers to the concept of using them in conjunction to represent language)

    As far as which is better, neither is really, 'better' in general. In the context of C++, C++ strings are probably a better choice, as they take advantage of the OO nature of C++ and the power that comes with it. In the context of C, obviously C-strings are the ONLY choice. Beyond that, it's a matter of personal choice. I use C-strings simply because I'm used to working with them so I don't mind the extra work, and because it's one step to have code that can compile as both C and C++.
    Last edited by sean; 12-24-2004 at 07:44 PM.

  3. #3
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Quote Originally Posted by sean_mackrory
    More accurately a C-string is the array of chars
    Even more accurately a c-string is a null terminated array of chars.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Even more accurately a c-string is a null terminated array of chars.
    As opposed to a counted string, or a high-order bit string, or a record based string, or a hybrid format. The trick to understanding C-style strings is to realize that it's just an array with a special way of marking how many characters are there. There's a physical length (the total allocated memory for the array) and a logical length (the number of meaningful characters present in the array).

    If I wanted, I could make my own string standard as a length-prefix counted string:
    Code:
    #include <iostream>
    #include <cstring>
    
    using namespace std;
    
    int main()
    {
      char *p = new char[50];
    
      ++p;
      memcpy(p, "This is a test", 14);
      p[-1] = 14;
    
      cout<<"The string is: \"";
      for (int i = 0; i < p[-1]; i++)
        cout.put(p[i]);
      cout<<'\"'<<endl;
    }
    All I'm doing is manipulating the cells of an array. As long as there's a way to tell how many meaningful characters are in the array, it's a string. The standard C-style string uses a null character ('\0') as a sentinel at the end to signify the end of the sequence.

    A C++ string is a data type that takes care of the low level stuff for you. Where you would have to micro-manage the null character and array size in a C-style string, a C++ string object does all of this behind the scenes. This leaves you free to solve more interesting problems.
    My best code is written with the delete key.

  5. #5
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    it's just an array with a special way of marking how many characters are there
    I just finished writing the request parser in my HTTP server - I learned A LOT about the limitations of the standard null-termination system. A word of advice: learn to be flexible.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >A word of advice: learn to be flexible.
    But only be flexible if the conventional way isn't practical. It's better to do things the way everyone expects unless you have an outstanding reason not to.
    My best code is written with the delete key.

  7. #7
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Indeed

  8. #8
    Registered User
    Join Date
    Nov 2004
    Posts
    25

    Smile

    O.o

    I understand the difference...I guess I just don't understand why using C++ strings the way I did is a problem.

    They are classes, and as such have boolean operators right?
    The only one I really need is '==' which I used, and it seemed to do what I wanted but...only once.

    Perplexing.

    Thanks anyways. =)

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >They are classes, and as such have boolean operators right?
    Not necessarily. Though == is overloaded for the string class.

    >The only one I really need is '==' which I used, and it seemed to do what I wanted but...only once.
    For a string object or a C-style string? If it's the latter then you got lucky with a pointer comparison. If it's the former then you probably didn't use it right.
    My best code is written with the delete key.

  10. #10
    Registered User
    Join Date
    Nov 2004
    Posts
    25
    The former. A C++ string's '==' operator is overloaded to compare the strings, obviously, but for some reason
    Code:
    while (buffer!="!"){
    only seemed to work once. Oh well, it's not of great importance, I'm just gonna try something different I think.

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Post the rest of the code. I imagine the problem was elsewhere and you randomly decided that it was the library and not your own code that was the problem.
    My best code is written with the delete key.

  12. #12
    Registered User
    Join Date
    Nov 2004
    Posts
    25

    Thumbs up

    The code's attatched to the thread I mentioned in the first post.
    And nah, I never blamed it on the library. I realize there's obviously something faulty with my code. =P That's why I posted it, so that maybe someone could point out what's wrong and I might learn from it. =)

  13. #13
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Fair enough. Now explain what you mean by "only seemed to work once". I'd rather go off of a short and detailed description of the problem than to go through the tedious steps of testing your relatively long code.
    My best code is written with the delete key.

  14. #14
    Registered User
    Join Date
    Nov 2004
    Posts
    25

    Talking

    Bleh, I guess it's not so much that it only works once, it just...barely works at all. =P In some cases, the first list will save correctly (by finding that it first actually does exist, then making a representation of the new save file without the list being overwritten, then it just appends the 'new' list to the end as if it were actually a new list.

    Also sometimes it hits an infinite loop which makes it keep 'skipping over' the last line. (usually the last contact's phone number...)

    I'm still not sure why this is since for that loop i'm using two conditions, one that checks to see if the next line should be skipped over, and the other to see if the fstream is at the end of file.

    Egh, this is all a little complicated for me, so maybe I should take it down a notch and practice something easier or something that doesn't require saving. Any advice?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help understanding arrays of strings!
    By smoking81 in forum C Programming
    Replies: 18
    Last Post: 02-23-2008, 04:24 AM
  2. Strings and Substrings using arrays
    By dcwang3 in forum C Programming
    Replies: 12
    Last Post: 02-18-2008, 07:28 AM
  3. arrays of strings
    By mbooka in forum C Programming
    Replies: 2
    Last Post: 02-19-2006, 07:55 PM
  4. Arrays of Strings
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 05-30-2002, 02:38 PM
  5. Structures Arrays and Char Strings
    By Crocksmyworld in forum C Programming
    Replies: 5
    Last Post: 01-19-2002, 11:56 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21