CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13
  1. #1
    Join Date
    Mar 2011
    Posts
    153

    re-initializing an array

    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;
    }

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: re-initializing an array

    Quote Originally Posted by heights View Post
    name[C] = {2} /* is this possible to re-initialize an array this way? one two and 9 zeroes */
    No.

    The term initialization has a specific meaning in C++. Nothing in C++ can be initialized more than once. Once something is initialized, it can only be changed using assignment.

    Regards,

    Paul McKenzie

  3. #3
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: re-initializing an array

    If you want you can use scoping rules to make one version of the variable go out of scope, and then bring another one in:

    Code:
    #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;
        }
        {
            char name[C] = {2};
    
            system("pause");
            return 0;
        }
    }
    Note these are completely separate arrays which just happen to have the same name.

  4. #4
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

    Re: re-initializing an array

    Code:
    char name[C] = {0}; // 10 zeroes
    
    //
    
    name[C] = {2} /* is this possible to re-initialize an array this way? one two and 9 zeroes */
    1. The second statement is only legal in C++0x

    2. The way that I read the standard ... the second statement is the same as :

    Code:
    name[C] = char(2);
    (which is out of bounds of the array).
    Last edited by Philip Nicoletti; June 22nd, 2011 at 05:52 AM.

  5. #5
    Join Date
    Mar 2011
    Location
    Delhi India
    Posts
    110

    Talking Re: re-initializing an array

    Quote Originally Posted by heights View Post
    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;
    }
    It has many 3 errors
    Code:
    Compiling...
    main.cpp
    ..............cpp(22) : error C2059: syntax error : '{'
    ..............cpp(22) : error C2143: syntax error : missing ';' before '{'
    ..............cpp(22) : error C2143: syntax error : missing ';' before '}'
    in line
    Code:
    name[C] = {2} /* is this possible to re-initialize an array this way? one two and 9 zeroes */
    you are trying to access 20th data of name array but is that exist. NO. Your program may crach in such condition.
    I like the answer of paul he says about initialization meaning.
    Quote Originally Posted by Paul McKenzie View Post
    The term initialization has a specific meaning in C++. Nothing in C++ can be initialized more than once. Once something is initialized, it can only be changed using assignment.
    heights you are understanding wrong meaning of initialization.
    If you want to change all the data in array to 2 then try this code
    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;
    
            for(int i=0;i<20;i++)
            name[i]='2';
            return 0;
    }
    one basic question to you in red region i put name[i]='2' not name[i]=2 why. what will change if i do so.
    Last edited by vkash; June 23rd, 2011 at 06:46 AM. Reason: add question

  6. #6
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: re-initializing an array

    Quote Originally Posted by vkash View Post
    heights you are understanding wrong meaning of initialization.
    If you want to change all the data in array to 2 then try this code
    But what the OP understood correctly and you did not, is that the code
    Code:
    char name[10] = {2};
    does not initialize the array with ten 2s.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  7. #7
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: re-initializing an array

    Quote Originally Posted by vkash View Post
    It has many 3 errors
    Code:
    Compiling...
    main.cpp
    ..............cpp(22) : error C2059: syntax error : '{'
    ..............cpp(22) : error C2143: syntax error : missing ';' before '{'
    ..............cpp(22) : error C2143: syntax error : missing ';' before '}'
    in line
    Code:
    name[C] = {2} /* is this possible to re-initialize an array this way? one two and 9 zeroes */
    you are trying to access 20th data of name array but is that exist. NO. Your program may crach in such condition.
    I like the answer of paul he says about initialization meaning.


    heights you are understanding wrong meaning of initialization.
    If you want to change all the data in array to 2 then try this code
    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;
    
            for(int i=0;i<20;i++)
            name[i]='2';
            return 0;
    }
    one basic question to you in red region i put name[i]='2' not name[i]=2 why. what will change if i do so.
    It's great that you're trying to help, but by the questions you ask, I'm not sure you're ready. You may want to look at this table to find out the difference between the character 2 and the numeric value of 2.

    http://www.asciitable.com/

    Also, name[C] is the 21st element of name, not the 20th.
    Last edited by GCDEF; June 23rd, 2011 at 12:04 PM.

  8. #8
    Join Date
    Mar 2011
    Location
    Delhi India
    Posts
    110

    Resolved Re: re-initializing an array

    Quote Originally Posted by GCDEF View Post
    It's great that you're trying to help, but by the questions you ask, I'm not sure you're ready. You may want to look at this table to find out the difference between the character 2 and the numeric value of 2.

    http://www.asciitable.com/

    Also, name[C] is the 21st element of name, not the 20th.
    GCDEF when i start my C++ i have learnt these things carefully and i know these things still now also.I ask his question to heights because i want to tell him are you perfect in this thing.
    My answer to my own question is
    when we write
    Code:
     for(int i=0;i<20;i++)
            name[i]='2'
    here name [i] is set to 2 but when we type
    Code:
    for(int i=0;i<20;i++)
            name[i]=2
    here we are telling to set name[i] to that value whose number in ASCII standards is 2. there are 256 such characters. these characters repeat after 256(-128 to 128). I have got this info from my VC++ code that is
    Code:
    #include<iostream>
    using namespace std;
    int main()
    {
    	for(int i=-150;i<128;i++)
    		cout<<i<<"  "<<char(i)<<endl;
    	return 0;
    }
    you have good link but i had already got all those things.
    Quote Originally Posted by vkash
    you are trying to access 20th data of name array but is that exist
    Quote Originally Posted by GCDEF
    Also, name[C] is the 21st element of name, not the 20th.
    heights i am sorry for wrong answer.
    shiiiit i go here in bjarne stroustrup method of counting not general counting method. I don't know why bjarne stroustrup break convention to start counting from 0 rather than 1. there may be several reasons like first ten have single digit in such counting method.
    Last edited by vkash; June 23rd, 2011 at 12:39 PM.

  9. #9
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: re-initializing an array

    Quote Originally Posted by vkash View Post
    GCDEF when i start my C++ i have learnt these things carefully and i know these things still now also.I ask his question to heights because i want to tell him are you perfect in this thing.
    My answer to my own question is
    when we write
    Code:
     for(int i=0;i<20;i++)
            name[i]='2'
    here name [i] is set to 2 but when we type
    Code:
    for(int i=0;i<20;i++)
            name[i]=2
    here we are telling to set name[i] to that value whose number in ASCII standards is 2. there are 256 such characters. these characters repeat after 256(-128 to 128). I have got this info from my VC++ code that is
    Code:
    #include<iostream>
    using namespace std;
    int main()
    {
    	for(int i=-150;i<128;i++)
    		cout<<i<<"  "<<char(i)<<endl;
    	return 0;
    }
    you have good link but i had already got all those things.


    heights i am sorry for wrong answer.
    ****i go here in bjarne stroustrup method of counting not general counting method. I don't know why bjarne stroustrup break convention to start counting from 0 rather than 1. there may be several reasons like first ten have single digit in such counting method.
    Oh dear. We don't use profanity in this forum.

    Stroustrup has nothing to do with array indexes beginning with 0. Think of the index as an offset from the start of the array, not an index number.

    You still seem a little confused between the difference between the number 2 and the character 2.

  10. #10
    Join Date
    Mar 2011
    Location
    Delhi India
    Posts
    110

    Cool Re: re-initializing an array

    Quote Originally Posted by GCDEF View Post
    Oh dear. We don't use profanity in this forum.

    .
    I have no intention of abusing(profanity) here.
    I don't abuse in real life as well as in any public internet place like this this. I write this word here because i think it's meaning is different. now i know that it is profanity in modern English.

    Quote Originally Posted by GCDEF View Post
    You still seem a little confused between the difference between the number 2 and the character 2
    I don't thing i am confused between these number2 and character 2.
    if you think so then explain it in your words. heights will also get benefit from from this.
    Last edited by vkash; June 23rd, 2011 at 03:09 PM.

  11. #11
    Join Date
    Feb 2002
    Posts
    4,640

    Re: re-initializing an array

    Quote Originally Posted by vkash
    My answer to my own question is
    when we write
    Code:
     for(int i=0;i<20;i++)
            name[i]='2'
    here name [i] is set to 2 but when we type
    Code:
    for(int i=0;i<20;i++)
            name[i]=2
    here we are telling to set name[i] to that value whose number in ASCII standards is 2. there are 256 such characters. these characters repeat after 256(-128 to 128).
    Code:
    int nSomeInt= '2';
    Here, 'nSomeInt' is not the value 2, it is 50.
    Code:
    int nSomeInt= 2;
    Here, 'nSomeInt' is the value 2.

    This is where you appear confused.

    Viggy
    Last edited by MrViggy; June 23rd, 2011 at 03:16 PM.

  12. #12
    Join Date
    Apr 1999
    Posts
    27,449

    Re: re-initializing an array

    Quote Originally Posted by vkash View Post
    I don't know why bjarne stroustrup break convention to start counting from 0 rather than 1.
    C++ has its roots in the 'C' language, and in 'C', array indices start from 0. The inventors of the C language are Brian Kernighan and Dennis Ritchie, not Bjarne Stroustrup.

    Regards,

    Paul McKenzie

  13. #13
    Join Date
    Mar 2011
    Location
    Delhi India
    Posts
    110

    Red face Re: re-initializing an array

    Quote Originally Posted by Paul McKenzie View Post
    C++ has its roots in the 'C' language, and in 'C', array indices start from 0. The inventors of the C language are Brian Kernighan and Dennis Ritchie, not Bjarne Stroustrup.

    Regards,

    Paul McKenzie
    OK.
    Quote Originally Posted by MrViggy View Post
    .
    I am not confused. It is the way of my writing that giving you impression that i am confused. My bad knowledge of English is also responsible for this.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured