Thread: char[] acting weird

  1. #1
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001

    char[] acting weird

    Ok, heres a little bit of code. The player enters a word, then I want to create a string (guessword) the same length, but have all of its elements set to '-'. It doesn't work. I get a bunch of weird characters:
    char word[MAX_LEN]; //Players secret word
    	char guessword[MAX_LEN]; //Duplicate word
    	char input; //Input char
    	bool win = false; //Has the player won?
    	int wrong = 0;
    	cout<<"Enter the secret word to be guessed: "; //Display command
    	cin.getline(word, MAX_LEN); //Get secret word
    	int len = strlen(word);
    	for(int i = 0; i < len; i++) //Set array to all dashes
    		strcat(guessword, "-");

  2. #2
    Registered User
    Join Date
    May 2003
    for(int i = 0;i < len;i++)
        guessword[i] = '-';
    guessword[len] = 0;

  3. #3
    Hardware Engineer
    Join Date
    Sep 2001

    "Null-terminated" strings

    These (c-style) strings are called "null-terminated" strings because they end with '\0'. The value of '\0' is zero.

    strlen() and cout look for the \0 to find the end of the string. If you put a \0 in the middle of a string. These functions will use that position as the end of the string. (Actually, the character just prior to the \0 is the end.)

    Note that this is the number zero... Not an ASCII character-zero!
    An ASCII zero has a decimal value of 48.

    0 = 0
    '\0' = 0
    '0' = 48
    "0" = 48, 0 (A string literal... two characters including the null termination)

  4. #4
    Registered User
    Join Date
    May 2003
    char word[MAX_LEN]; //Players secret word
    char guessword[MAX_LEN]; //Duplicate word
    for(int i = 0; i < len; i++) //Set array to all dashes
        strcat(guessword, "-");
    Here's a Q: What does guessword contain when it's initially created, and not initialized? The answer: Undefined.

    Now you're trying to concatenate a known string onto the end of a buffer which could contain anything and may well not even be a valid string (a valid string has to have the null terminator before the end of the buffer).

    Guessword needs to be initialized to a null string if you expect to concatenate to it. That garbage you're seeing is what guessword started out as. Unless you specifically initialize your buffer, it's going to contain random crap. This is beneficial from a performance viewpoint; initializing memory to some known value, like all zeroes, takes time. And they provide the programmer the means to do this if they want, so you get the best of both worlds -- if you don't need to initialize, you save time, and if you do need to, you always can. In your case, you only would need to initialize one element of guessword, anyway -- and you can, without having to initialize the rest.

    But you *didn't* initialize the buffer, so it's going to contain whatever random garbage was at that memory location before your buffer. If you want the buffer to contain a null string ("" AKA '\0'), well, you need to tell it that.

    Of course, you'd also be advised to use C++ string manipulation methods, not C-style ones, but you should understand how to use the C-style as well.
    Last edited by Cat; 06-09-2003 at 06:53 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Floating Values acting weird.
    By torqu3e in forum C Programming
    Replies: 9
    Last Post: 12-19-2006, 11:12 AM
  2. atof() acting weird?
    By Mellowz in forum C Programming
    Replies: 4
    Last Post: 07-09-2006, 03:48 AM
  3. DirectX 9 materials acting weird
    By Rune Hunter in forum Game Programming
    Replies: 7
    Last Post: 12-27-2005, 12:07 AM
  4. Replies: 18
    Last Post: 06-21-2003, 10:57 AM
  5. n00b needs help with api graphics acting very weird
    By CheeseWeaver in forum Windows Programming
    Replies: 2
    Last Post: 03-18-2003, 03:15 PM