Thread: More problems with C style strings

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    11

    More problems with C style strings

    I'm writing a small function to retrieve the first line from a C style string passed (I have my own reasons). Here's the code so far, in essence:
    Code:
    char* getaline(char const* const from, char* to) {
      char line = 0;
      for(unsigned int pos = 0; pos < strlen(from); pos++) {
        if(from[pos] == '\n') break;
        cout << "\tfrom[pos] = \"" << from[pos] << "\"";
        line += from[pos];
        cout << "\t\tline = \"" << line << "\"\n";
      };
    }
    This is really odd. The character inside the quotes after 'from[pos] = ' is what I would expect it to be, but then in the second column (displaying what's in the buffer "line") is a mix of random symbols, one per line, including a musical note and an up-down arrow.

    Can someone tell me what's going on?

  2. #2
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    line is a single char. When you do +=, I assume it just adds to the ASCII code for the char. So:

    line = 'a';
    line += 'd';


    Makes line = to whatever character corresponds to 197 or whatever the ASCII code for 'a' and 'd' are added together.

    If you want line to be a c style string, you can't use +=. You'll have to use strcat or make your own if you don't want to use <cstring>.

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    11
    Thanks for the quick reply - just one more:

    strcat(line, from[pos]); gives the error 'Could not find a match for strcat(char, const char)'.

    I included <cstring>.

  4. #4
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Originally posted by jlou
    line is a single char.
    You still have line declared as a char. There is a difference between a char and an array of char. An array of char is one or more chars that form a c style string. An array of chars is either char* or char[], but never just char. Since line is declared only as a char, you can't add on to it. You also can't strcat to it, because it is a single char, not a string.

    You need to make line a c style string, allocate memory for it, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BN_CLICKED, change button style
    By bennyandthejets in forum Windows Programming
    Replies: 13
    Last Post: 07-05-2010, 11:42 PM
  2. Problems with strings as key in STL maps
    By all_names_taken in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:34 AM
  3. WinMain and return msg.wParam problems
    By Manaxter in forum C++ Programming
    Replies: 4
    Last Post: 01-08-2006, 11:58 AM
  4. Problems about gcc installation
    By kevin_cat in forum Linux Programming
    Replies: 4
    Last Post: 08-09-2005, 09:05 AM
  5. Problems with Strings and Arrays.
    By SlyMaelstrom in forum C++ Programming
    Replies: 13
    Last Post: 04-15-2005, 02:13 PM