Thread: default intialization of an array

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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

  2. #2
    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.

  3. #3
    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'.

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