Retarded (most likely) question about strings.

This is a discussion on Retarded (most likely) question about strings. within the C Programming forums, part of the General Programming Boards category; Howdy. I'm having a little difficulty with strings, so hopefully somebody here can steer me in the right direction Anyways, ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Location
    Edmonton, Alberta, Canada
    Posts
    3

    Retarded (most likely) question about strings.

    Howdy. I'm having a little difficulty with strings, so hopefully somebody here can steer me in the right direction

    Anyways, lets say I create a string:

    Code:
    char str[500];
    Now, lets say I need to store user input in this string, and go about it by using getchar(), and placing each character individually in the array. Now, I know that in order to be able to use this array as a string, it needs to contain '\0' at the end of the string. However, what exactly is the end of the string?

    For example, if 150 characters end up getting placed in the array. Is the end of the string considered to be right after the 150th character, or is the end of the string at location str[499]?

    If the former is true, I suppose it would be easy enough to place the null character right after you finish reading input. But if the latter is true, I figure it would be easiest to just initialize the entire array as null right off the bat. If so, how could you do that?

    Can you initialize it when you create it by going:

    Code:
    char str[500] = {'\0'};
    Since you only provide one character, would it set the rest of the array to the same character? If not, would you have to create a seperate function to loop through each element and set it to null?

    I'm not sure if I'm complicating this too much or not, but it's confusing me substantially. Thanks for any help!

  2. #2
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    char str[500]; doesn't create a string, it creates an array of 500 chars. It only becomes a string when it is terminated by a null. In answer to your first question, the end of the string is straight after the last character you want to be in the string.

    In answer to your second question, yes, you can initialise it with
    Code:
    char str[500] = {'\0'};
    If you initialise any elements of an array, the rest are treated as though they are initialised to 0.

  3. #3
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,807
    That would work in this case basically what is happening is it sets the first value to '\0' then the rest to 0 which is the same in this case example
    Code:
    #include <stdio.h>
    
    int main()
    {
        int i = 0, nullCounter = 0;
        char someString[200] = {'\0'};
        for(i = 0; i < 200; i++)
        {
            if(someString[i] == '\0')
            {
                nullCounter++;
            }
        }
        printf("nullCounter = %i",nullCounter);
        getchar();
        
        return 0;
    }
    But what happens when I do this
    Code:
    #include <stdio.h>
    
    int main()
    {
        int i = 0, nullCounter = 0;
        char someString[200] = {'\n'};
        for(i = 0; i < 200; i++)
        {
            if(someString[i] == '\0')
            {
                nullCounter++;
            }
        }
        printf("nullCounter = %i",nullCounter);
        getchar();
        
        return 0;
    }
    What will nullCounter be?
    Woop?

  4. #4
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    199, because as I said above, if any elements are initialised in an array, the rest will be set to 0.

  5. #5
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,807
    I wasn't posting to you my friend .
    Woop?

  6. #6
    Registered User
    Join Date
    Oct 2005
    Location
    Edmonton, Alberta, Canada
    Posts
    3
    Ah, ok. I understand now. I just really wasn't sure on how it handled the rest of the elements if you only initialize one. However, now I know that the remaining elements are set to null.

    One more question though. Lets say you don't initialize the array to nulls like your above examples. You then prompt the user for some input (lets say using scanf), storing the input into the array. He enteres "Hot mama" then hits return. What is now stored in the array?

    Is it: ['H', 'o', 't', ' ', 'm', 'a', 'm', 'a', '\n'], thus requiring you to add a null character yourself if you want to use the array as a string?

    Or, is it: ['H', 'o', 't', ' ', 'm', 'a', 'm', 'a', '\n', '\0'], thus letting you use the array as a string right away?

    Thanks again guys (or gals).
    Last edited by Kris; 10-13-2005 at 12:01 AM.

  7. #7
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    473
    Actually it will be 'H', 'o', 't', '\0', scanf will append the \0 but will only read until the first whitespace character it finds.

  8. #8
    Registered User
    Join Date
    Oct 2005
    Location
    Edmonton, Alberta, Canada
    Posts
    3
    Haha. Oh man, forgot about that. Don't I look silly now

    Sorry for the stupid questions. Thanks though

  9. #9
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Quote Originally Posted by nonpuz
    Actually it will be 'H', 'o', 't', '\0', scanf will append the \0 but will only read until the first whitespace character it finds.
    That's true if you use %s, but:

    Code:
    #include <stdio.h>
    
    int main(void)
    {
            char foo[201];
    
            scanf("%200[^\n]", foo);
            printf("foo is \"%s\"\n", foo);
    
            return 0;
    }
    My output:
    Code:
    hot mama
    foo is "hot mama"
    Edit:
    Quote Originally Posted by Kris
    Sorry for the stupid questions
    Your questions are an order of magnitude smarter than ones from a lot of new posters.

  10. #10
    Registered User Baaaah!'s Avatar
    Join Date
    Oct 2005
    Location
    UK
    Posts
    23
    cwr: does '[^\n]' mean, ignore newline character?

  11. #11
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    It means keep reading any character until a newline is reached.

  12. #12
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    473
    Quote Originally Posted by cwr
    That's true if you use %s, but:

    Code:
    #include <stdio.h>
    
    int main(void)
    {
            char foo[201];
    
            scanf("%200[^\n]", foo);
            printf("foo is \"%s\"\n", foo);
    
            return 0;
    }
    My output:
    Code:
    hot mama
    foo is "hot mama"
    Indeed, I was incorrect in assuming he would use %s. Although I'd wager it was the most likely case.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about strings in c
    By gp364481 in forum C Programming
    Replies: 9
    Last Post: 11-13-2008, 05:32 PM
  2. Question About Strings
    By spanker in forum C++ Programming
    Replies: 1
    Last Post: 07-13-2008, 05:09 AM
  3. strings question
    By cstudent in forum C Programming
    Replies: 4
    Last Post: 04-18-2008, 07:28 AM
  4. Functions and Strings Question
    By StrikeMech in forum C Programming
    Replies: 4
    Last Post: 07-18-2007, 06:07 AM
  5. Strings question
    By kimimaro in forum C Programming
    Replies: 10
    Last Post: 03-14-2005, 11:14 PM

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