Thread: default intialization of an array

  1. #16
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I don't know what comment you want about the comment. It is very true. The difference from before (I guess this is what you're asking) is that since you're building this string from scratch rather than reading it in, you have to build the \0 character as you're building the string.

  2. #17
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Quote Originally Posted by tabstop View Post
    I don't know what comment you want about the comment. It is very true. The difference from before (I guess this is what you're asking) is that since you're building this string from scratch rather than reading it in, you have to build the \0 character as you're building the string.
    Thank you very much.

    By the phrase "reading it in" I think you mean that inputting something using keyboard etc.

    If I had used the function strcpy() then I think it would have automatically inserted '\0' at the end.
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  3. #18
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    tabstop, please let me know whether what I said above is correct... Thank you.
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  4. #19
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    You know you could have googled the answer faster than you typed out that post, right?
    This was in the first hit: (strcpy)
    The strcpy() function shall copy the string pointed to by s2 (including the terminating null byte) into the array pointed to by s1. If copying takes place between objects that overlap, the behavior is undefined.

  5. #20
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Quote Originally Posted by jackson6612
    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 going to assume an ASCII based character set and ask you to try this program:
    Code:
    #include <iostream>
    
    using namespace std;
    
    const int C = 5;
    char str[C] = {74, 97, 99, 107, 0};
    
    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;
    
        return 0;
    }
    Why doesn't cout'ing produce "7497991070" instead of "Jack". Do you get me?
    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

  6. #21
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Quote Originally Posted by jackson6612 View Post
    Thank you very much.

    By the phrase "reading it in" I think you mean that inputting something using keyboard etc.
    That's all you would need to worry about at the moment, I think, but "it" can also refer to files.

    If I had used the function strcpy() then I think it would have automatically inserted '\0' at the end.
    Your manual doesn't agree. For an example reference see this. Basically, strcpy only works as long as dest can accommodate src, and if src is '\0' terminated.

    Feel free to use codecogs, (or really any other C reference) to answer any library questions you may have. Codecogs isn't the most complete reference I've ever seen by the way, but it's the easiest one to navigate so I HTH. I usually just use Google.

    It's a dying art I know.

  7. #22
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Hi

    Please have a look on the embedded questions in the code below. Thank you for all the help.

    Code:
    // learning initialization.cpp
    
    #include <iostream>
    #include <cstdlib>
    #include <cstring>
    
    using namespace std;
    
    const int C = 20;
    
    int main()
    {
            char name[C] = {0}; // 10 zeroes
            int v = 10;
    
            cout << v << endl;
    
            v = 20;
    
            cout << v << endl;
            cout << name << endl;
    
            name[C] = {2} /* is this possible to re-initialize an array this way? one two and 9 zeroes */
    
            system("pause");
            return 0;
    }
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  8. #23
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I don't think a single comment in this code is correct. I don't know how many times it has to be said that you cannot try to affect more than one slot in an array at a time, except for the special case of initialization, and for the sake of me not liking to report people's posts I hope this is the last time.

  9. #24
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by tabstop View Post
    I don't know how many times it has to be said that you cannot try to affect more than one slot in an array at a time, except for the special case of initialization, and for the sake of me not liking to report people's posts I hope this is the last time.
    I almost hate to do this, because it's going to confuse the OP to no end, and he really shouldn't do this, but...
    Code:
    union
    {
        smalltype *sp;
        biggertype *bp;
    } u;
    
    smalltype sa[x];
    u.sp = sa;
    u.bp[0] = 0; /* <-- */
    Almost.

    And no, this doesn't reinitialize the array, so don't ask if it does OP.


    Quzah.
    Hope is the first step on the road to disappointment.

  10. #25
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by quzah View Post
    I almost hate to do this, because it's going to confuse the OP to no end, and he really shouldn't do this, but...
    ...snip...
    Whoa, you're handing out loaded guns now huh?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  11. #26
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    >>name[C] = {2};
    I fail to see why you cannot understand that you cannot assign multiple elements or an array to a single char.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

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