Like Tree1Likes

default intialization of an array

This is a discussion on default intialization of an array within the C++ Programming forums, part of the General Programming Boards category; I don't know what comment you want about the comment. It is very true. The difference from before (I guess ...

  1. #16
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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
    21,777
    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?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #21
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,709
    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,185
    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.
    tabstop likes this.
    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,305
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,630
    >>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.

Page 2 of 2 FirstFirst 12
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, 01:20 PM

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