Thread: default intialization of an array

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    254

    default intialization of an array

    Hi

    I was trying to check what arrays are initialized to by default. In my case for the code below the string array was initialized to white spaces because this is what I got. Would you please tell me what arrays (C-strings are also arrays) are initialized to by default. Thanks for the help.

    Code:
    // learning_arrays_strings.cpp
    // intializtion of an array
    
    #include <iostream>
    #include <cstdlib>
    #include <cstring>
    
    using namespace std;
    
    const int C = 5;
    char str[C];
    
    int main()
    
    {
            int i;
    
            for (i=0; i<C; i++)
            {
                    cout << str[i];
            }
    
            cout << endl;
    
            system("pause");
            return 0;
    }
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You should be careful what you mean by white spaces. If you think you mean the character you get by pushing the big bar at the bottom of your keyboard, then that's not it.

    All variables (arrays or no) that are declared at file scope (i.e. outside main) are initialized to 0 (that's the character 0, sometimes spelled '\0', not the character '0' which is 48). Any variables declared inside main (arrays or no) are not initialized to anything unless you do it explicitly.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Quote Originally Posted by tabstop View Post
    You should be careful what you mean by white spaces. If you think you mean the character you get by pushing the big bar at the bottom of your keyboard, then that's not it.

    All variables (arrays or no) that are declared at file scope (i.e. outside main) are initialized to 0 (that's the character 0, sometimes spelled '\0', not the character '0' which is 48). Any variables declared inside main (arrays or no) are not initialized to anything unless you do it explicitly.
    Thanks, tabstop.

    Okay. White space is not the space which doesn't contain anything. But the space one get by pressing space bar is different. It determines the space between two outputs.

    The output for the code I mentioned in the my first post is:
    Code:
    Press any key to continue . . .
    You see the white space I get. If the array elements were initialized to zero then why am I getting spaces? Please let me know. Thanks a lot.
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by jackson6612
    You see the white space I get.
    I don't.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Yeah, that looks like no spaces at all to me. The "Press any key to continue..." is printed by your system("pause").

  6. #6
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Quote Originally Posted by laserlight View Post
    I don't.
    Hi laserlight

    How could you see white spaces? You could only observe them. I'm sorry for my bad choice of words.

    EDIT: Please have a look on the attached screenshot. You see there is blank space appearing above "Press any key to continue ...".
    Attached Images Attached Images default intialization of an array-whitespace-jpg 
    Last edited by jackson6612; 06-16-2011 at 11:52 AM.
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    If you want to make things clear, write something along these lines:
    Code:
    #include <iostream>
    #include <cstdlib>
    
    using namespace std;
    
    const int C = 5;
    char str[C];
    
    int main()
    {
        cout << '"';
        for (int i = 0; i < C; ++i)
        {
            cout << str[i];
        }
        cout << '"' << endl;
    
        for (int i = 0; i < C; ++i)
        {
            cout << static_cast<int>(str[i]);
        }
        cout << endl;
    
        cout << static_cast<int>(' ') << endl;
    
        system("pause");
        return 0;
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    That blank line is printed by your cout << endl.

  9. #9
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Quote Originally Posted by laserlight View Post
    If you want to make things clear, write something along these lines:
    Code:
    #include <iostream>
    #include <cstdlib>
    
    using namespace std;
    
    const int C = 5;
    char str[C];
    
    int main()
    {
        cout << '"';
        for (int i = 0; i < C; ++i)
        {
            cout << str[i];
        }
        cout << '"' << endl;
    
        for (int i = 0; i < C; ++i)
        {
            cout << static_cast<int>(str[i]);
        }
        cout << endl;
    
        cout << static_cast<int>(' ') << endl;
    
        system("pause");
        return 0;
    }
    Thanks a lot for the code, laserlight. It was helpful.

    First let me clear this out. The int 0, NULL character '\0', and character '0' are different from each other. Correct?

    Anything which has been declared globally is initialized to zero, so every element of str[C] in your code is 0. Then, why doesn't cout'ing produces a series of 0's instead of spaces. Do you get me?
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    int 0 and NULL '\0' are the same. '0' is different (usually 48). Since the string contains '\0' and not '0', therefore you are not printing '0'.

  11. #11
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Quote Originally Posted by tabstop View Post
    int 0 and NULL '\0' are the same. '0' is different (usually 48). Since the string contains '\0' and not '0', therefore you are not printing '0'.
    Thanks a lot for helping me with my countless seemingly silly questions.

    Both var and str[C] have been declared globally. I get "0" for var but for str[C] I get spaces. Why is so?

    As C=15 therefore I was expecting to get series of 14 zeroes (14 and not 15 because 15th element of the array would contain NULL).

    I hope you understand where I'm having difficulty. Thank for the help.

    Code:
    // learning_arrays_strings.cpp
    // intializtion of an array
    
    #include <iostream>
    #include <cstdlib>
    #include <cstring>
    
    using namespace std;
    
    int var;
    const int C = 15;
    char str[C];
    char name[C];
    
    int main()
    
    {
            int i;
    
            cout << var << "hello there" << endl;
    
            for (i=0; i<C; i++)
            {
                    cout << str[i];
            }
    
            cout << "hello there" << endl;
    
            cout << "enter your name: ";
            cin.get(name, C);
    
            cout << name << endl;
    
            system("pause");
            return 0;
    }
    Code:
    0hello there
                   hello there
    enter your name:
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by jackson6612 View Post
    I get "0" for var but for str[C] I get spaces. Why is so?
    Because, still, you don't get spaces. There is probably some weird C++ thing about << and characters that is going on here. (<< and characters are always a little weird, IMO.) Change the loop to just
    Code:
    cout << str;
    and you will see nothing print.

  13. #13
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Quote Originally Posted by tabstop View Post
    Because, still, you don't get spaces. There is probably some weird C++ thing about << and characters that is going on here. (<< and characters are always a little weird, IMO.) Change the loop to just
    Code:
    cout << str;

    and you will see nothing print.
    Hi tabstop

    Thank you for the help. I'm happy that my question wasn't that much silly this time.

    1: If those are not spaces, then what are they? Could you please tell me?

    2: Within a loop I have to have some kind of index so that looping can go on. Changing it to just " cout << str; " would obviously do nothing because there is no index variable such as "i". What were you trying to teach me then?!

    3: I have a major test tomorrow so please help me with this and please don't get frustrated. Does the compiler automatically insert NULL character at the end array of char type which is a C-string?

    Code:
    char name[40];
    
    cout << "enter your name";
    cin.getline(name, 40);
    
    cout << name << endl;
    Suppose my name is "Jackson Heights" which is 15 characters long, then would the compiler automatically insert NULL terminator at 16th position or would it insert the NULL at 40th position?

    Thanks a lot for your time and help.
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    1. I have no idea what it's trying to print. There's probably some sort of conversion going on.
    2. Since you're supposed to take the loop out, that's pretty irrelevant.
    3. Yes. The \0 character always goes at the end of the input. It would be really bad for your string to be "Jackson Heightsßßßßßßßßßßßßßßßßßßßßßßßß".

  15. #15
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Quote Originally Posted by tabstop View Post
    1. I have no idea what it's trying to print. There's probably some sort of conversion going on.
    2. Since you're supposed to take the loop out, that's pretty irrelevant.
    3. Yes. The \0 character always goes at the end of the input. It would be really bad for your string to be "Jackson Heightsßßßßßßßßßßßßßßßßßßßßßßßß".
    Hi tabstop

    Please comment on the embedded comment in the code below. Thanks.


    Code:
    // strcopy1.cpp
    // copies a string using a for loop
    
    #include <iostream>
    #include <cstdlib>
    #include <cstring>
    
    using namespace std;
    
    int main()
    {
            const int MAX = 80;
            int i;
    
            char str1[] = "Oh, Captain, my Captain! "
                          "\nOur fearful trip is done";
    
            /*
            A C-String could be initialized in two ways:
            1: char str[] = "hello world";
            2: char str[] = {'h','e','l','l','o',' ', 'w','o','r','l','d','\0'};
    
            Just leave the square brackets empty []
            */
    
            char str2[MAX];
    
            for(i=0; i<strlen(str1); i++)
            {
                str2[i] = str1[i];
            }
    
            /*
            Although NULL terminator would be inserted automatically
            but only after 79 elements have been entered.
    
            If str1 was 20 elements long, then (79 - 20) elements would
            contain garbage values. Therefore, one has to enter NULL
            explicitly.
            */
    
            str2[i] = '\0';
    
            cout << str2 << endl;
    
            system("pause");
            return 0;
    }
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. comma at end of intialization list
    By matsp in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2008, 01:13 PM
  2. Creating array of objects w/o default constructor
    By QuestionC in forum C++ Programming
    Replies: 19
    Last Post: 05-02-2007, 08:03 PM
  3. Arrays Indexing Intialization
    By jshamilton73 in forum C++ Programming
    Replies: 7
    Last Post: 06-06-2003, 02:26 PM
  4. [] intialization of arrays
    By Saravanan in forum C Programming
    Replies: 3
    Last Post: 11-25-2002, 02:20 PM