Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Array Variable initialization

Array Variable initialization

Scheduled Pinned Locked Moved C / C++ / MFC
data-structureshelptutorialquestion
25 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T T RATHA KRISHNAN

    Hi! I've initialized an Array element in the Constructor. But it shows syntax error. TModels[1](0); The error is: error C2059: syntax error : '[' How to initialize an array element in the Constructor?

    T Offline
    T Offline
    toxcct
    wrote on last edited by
    #2

    can you show your constructor please ?

    T.RATHA KRISHNAN wrote:

    TModels[1](0);

    this line makes absolutely no sense, and i don't even know what you're trying to achieve with this. BTW, can't you just use the STL ans its containers ?

    [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

    T 1 Reply Last reply
    0
    • T toxcct

      can you show your constructor please ?

      T.RATHA KRISHNAN wrote:

      TModels[1](0);

      this line makes absolutely no sense, and i don't even know what you're trying to achieve with this. BTW, can't you just use the STL ans its containers ?

      [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

      T Offline
      T Offline
      T RATHA KRISHNAN
      wrote on last edited by
      #3

      Thanks. I've corrected this error myself.

      T 1 Reply Last reply
      0
      • T T RATHA KRISHNAN

        Thanks. I've corrected this error myself.

        T Offline
        T Offline
        toxcct
        wrote on last edited by
        #4

        and for the benefit of everyone, will you share this with us ?

        [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

        T 1 Reply Last reply
        0
        • T T RATHA KRISHNAN

          Hi! I've initialized an Array element in the Constructor. But it shows syntax error. TModels[1](0); The error is: error C2059: syntax error : '[' How to initialize an array element in the Constructor?

          L Offline
          L Offline
          laksh2204
          wrote on last edited by
          #5

          I think you are trying to do something like this.. class A{ int TModels[3]; public: A():TModels[1](0){} }; int main(){ A a; } which is giving you above error: As per my knowledge there is no (standard) C++ way of doing this. Array initializers do not exist for classes. So prob you can do it somewhat like this: class A{ int TModels[3]; public: A(){ for(int i=0; i<3; i++) TModels[i] = 0; } }; int main(){ A a; } Hope it helps you

          T 1 Reply Last reply
          0
          • T toxcct

            and for the benefit of everyone, will you share this with us ?

            [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

            T Offline
            T Offline
            T RATHA KRISHNAN
            wrote on last edited by
            #6

            Oh! With Pleasure! First I initialized the array elements like this:

            TestAI::TestAI() //Constructor
            TModels[1](0)
            , TModels[2](0)
            , TModels[3](0)
            {

            }

            and now I change it to the following:

            TestAI::TestAI() //Constructor
            {
            TModels[1] = 0;
            TModels[2] = 0;
            TModels[3] = 0;

            }

            and the errors vanished.

            T 1 Reply Last reply
            0
            • L laksh2204

              I think you are trying to do something like this.. class A{ int TModels[3]; public: A():TModels[1](0){} }; int main(){ A a; } which is giving you above error: As per my knowledge there is no (standard) C++ way of doing this. Array initializers do not exist for classes. So prob you can do it somewhat like this: class A{ int TModels[3]; public: A(){ for(int i=0; i<3; i++) TModels[i] = 0; } }; int main(){ A a; } Hope it helps you

              T Offline
              T Offline
              T RATHA KRISHNAN
              wrote on last edited by
              #7

              Thanks. I've already corrected that.

              1 Reply Last reply
              0
              • T T RATHA KRISHNAN

                Oh! With Pleasure! First I initialized the array elements like this:

                TestAI::TestAI() //Constructor
                TModels[1](0)
                , TModels[2](0)
                , TModels[3](0)
                {

                }

                and now I change it to the following:

                TestAI::TestAI() //Constructor
                {
                TModels[1] = 0;
                TModels[2] = 0;
                TModels[3] = 0;

                }

                and the errors vanished.

                T Offline
                T Offline
                toxcct
                wrote on last edited by
                #8

                why don't you just do this :

                TModel = {0};

                [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

                CPalliniC 1 Reply Last reply
                0
                • T toxcct

                  why don't you just do this :

                  TModel = {0};

                  [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

                  CPalliniC Offline
                  CPalliniC Offline
                  CPallini
                  wrote on last edited by
                  #9

                  Because it doesn't work? For instance

                  int a[50] = {0};

                  Doesn't produce the result I'm expecting (it initializes only the first array element). Am i wrong? :)

                  If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                  This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                  [My articles]

                  In testa che avete, signor di Ceprano?

                  T D 2 Replies Last reply
                  0
                  • CPalliniC CPallini

                    Because it doesn't work? For instance

                    int a[50] = {0};

                    Doesn't produce the result I'm expecting (it initializes only the first array element). Am i wrong? :)

                    If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                    This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                    [My articles]

                    T Offline
                    T Offline
                    toxcct
                    wrote on last edited by
                    #10

                    CPallini wrote:

                    Am i wrong?

                    you are, or the compiler is ! the standard says that such a construction (when initializing a variable at the same time than declaration) initializes every element of the array to their default value. I'm even pretty sure Nemanja Trifunovic quoted me somewhere about that...

                    [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

                    CPalliniC 1 Reply Last reply
                    0
                    • CPalliniC CPallini

                      Because it doesn't work? For instance

                      int a[50] = {0};

                      Doesn't produce the result I'm expecting (it initializes only the first array element). Am i wrong? :)

                      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                      [My articles]

                      D Offline
                      D Offline
                      David Crow
                      wrote on last edited by
                      #11

                      CPallini wrote:

                      (it initializes only the first array element).

                      Not according to the STOSx instructions. Of course, I only use it to initialize POD types to 0. Otherwise, I'd use memset().

                      "Love people and use things, not love things and use people." - Unknown

                      "The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch

                      CPalliniC 1 Reply Last reply
                      0
                      • T toxcct

                        CPallini wrote:

                        Am i wrong?

                        you are, or the compiler is ! the standard says that such a construction (when initializing a variable at the same time than declaration) initializes every element of the array to their default value. I'm even pretty sure Nemanja Trifunovic quoted me somewhere about that...

                        [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

                        CPalliniC Offline
                        CPalliniC Offline
                        CPallini
                        wrote on last edited by
                        #12

                        toxcct wrote:

                        you are, or the compiler is !

                        Maybe the latter: the program:

                        #include <iostream>
                        using namespace std;

                        void main()
                        {
                        const int N = 10;
                        int a[N]={7};

                        for (int i=0; i<N;i++)
                        {
                        cout << a[i] << endl;
                        }
                        }

                        the output:

                        7
                        0
                        0
                        0
                        0
                        0
                        0
                        0
                        0
                        0

                        The system: Visual C++ 2008 Express Edition running on Win XP. Eventually YOU may be wrong! ;P (actually I'm quite confident you're right, but don't use it unless you really want surprises!) [added] Actually I think VS2008 cannot be so out-of-the-standard. I suppose the standard establishing that, whenever the initialization list is too short, the remaining array items are default-initialized (to 0 if integers). [/added] :)

                        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                        [My articles]

                        modified on Tuesday, September 9, 2008 11:53 AM

                        In testa che avete, signor di Ceprano?

                        S 1 Reply Last reply
                        0
                        • D David Crow

                          CPallini wrote:

                          (it initializes only the first array element).

                          Not according to the STOSx instructions. Of course, I only use it to initialize POD types to 0. Otherwise, I'd use memset().

                          "Love people and use things, not love things and use people." - Unknown

                          "The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch

                          CPalliniC Offline
                          CPalliniC Offline
                          CPallini
                          wrote on last edited by
                          #13

                          Sorry no STOS here:

                          ...
                          const int N = 10;
                          int a[N]={7};
                          00401003 xor eax,eax
                          00401005 push esi
                          00401006 mov dword ptr [esp+8],7
                          0040100E mov dword ptr [esp+0Ch],eax
                          00401012 mov dword ptr [esp+10h],eax
                          00401016 mov dword ptr [esp+14h],eax
                          0040101A mov dword ptr [esp+18h],eax
                          0040101E mov dword ptr [esp+1Ch],eax
                          00401022 mov dword ptr [esp+20h],eax
                          00401026 mov dword ptr [esp+24h],eax
                          0040102A mov dword ptr [esp+28h],eax
                          0040102E mov dword ptr [esp+2Ch],eax
                          ...

                          (Visual C++ 2008 Express Edition, Win XP). :)

                          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                          This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                          [My articles]

                          In testa che avete, signor di Ceprano?

                          D 1 Reply Last reply
                          0
                          • CPalliniC CPallini

                            toxcct wrote:

                            you are, or the compiler is !

                            Maybe the latter: the program:

                            #include <iostream>
                            using namespace std;

                            void main()
                            {
                            const int N = 10;
                            int a[N]={7};

                            for (int i=0; i<N;i++)
                            {
                            cout << a[i] << endl;
                            }
                            }

                            the output:

                            7
                            0
                            0
                            0
                            0
                            0
                            0
                            0
                            0
                            0

                            The system: Visual C++ 2008 Express Edition running on Win XP. Eventually YOU may be wrong! ;P (actually I'm quite confident you're right, but don't use it unless you really want surprises!) [added] Actually I think VS2008 cannot be so out-of-the-standard. I suppose the standard establishing that, whenever the initialization list is too short, the remaining array items are default-initialized (to 0 if integers). [/added] :)

                            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                            This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                            [My articles]

                            modified on Tuesday, September 9, 2008 11:53 AM

                            S Offline
                            S Offline
                            SandipG
                            wrote on last edited by
                            #14

                            Same output with Visual C++ 6.0, WinXP SP2 :) Surprising..:confused:

                            Regards, Sandip.

                            T 1 Reply Last reply
                            0
                            • CPalliniC CPallini

                              Sorry no STOS here:

                              ...
                              const int N = 10;
                              int a[N]={7};
                              00401003 xor eax,eax
                              00401005 push esi
                              00401006 mov dword ptr [esp+8],7
                              0040100E mov dword ptr [esp+0Ch],eax
                              00401012 mov dword ptr [esp+10h],eax
                              00401016 mov dword ptr [esp+14h],eax
                              0040101A mov dword ptr [esp+18h],eax
                              0040101E mov dword ptr [esp+1Ch],eax
                              00401022 mov dword ptr [esp+20h],eax
                              00401026 mov dword ptr [esp+24h],eax
                              0040102A mov dword ptr [esp+28h],eax
                              0040102E mov dword ptr [esp+2Ch],eax
                              ...

                              (Visual C++ 2008 Express Edition, Win XP). :)

                              If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                              This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                              [My articles]

                              D Offline
                              D Offline
                              David Crow
                              wrote on last edited by
                              #15

                              CPallini wrote:

                              00401003 xor eax,eax

                              Look two lines up from this one and you should find:

                              rep stosd

                              "Love people and use things, not love things and use people." - Unknown

                              "The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch

                              CPalliniC 1 Reply Last reply
                              0
                              • D David Crow

                                CPallini wrote:

                                00401003 xor eax,eax

                                Look two lines up from this one and you should find:

                                rep stosd

                                "Love people and use things, not love things and use people." - Unknown

                                "The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch

                                CPalliniC Offline
                                CPalliniC Offline
                                CPallini
                                wrote on last edited by
                                #16

                                No luck (there isn't such insruction). On the other hand, the output of this program [^] confirms my assumption. See also Sandip's post [^]. I should admit I was very surprised by such a behaviour. :)

                                If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                                This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                                [My articles]

                                In testa che avete, signor di Ceprano?

                                T 1 Reply Last reply
                                0
                                • S SandipG

                                  Same output with Visual C++ 6.0, WinXP SP2 :) Surprising..:confused:

                                  Regards, Sandip.

                                  T Offline
                                  T Offline
                                  toxcct
                                  wrote on last edited by
                                  #17

                                  SandipG :) wrote:

                                  Same output with Visual C++ 6.0

                                  Visual C++ is not what a decent programmer call a standard compliant compiler, Sir !

                                  [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

                                  CPalliniC 1 Reply Last reply
                                  0
                                  • CPalliniC CPallini

                                    No luck (there isn't such insruction). On the other hand, the output of this program [^] confirms my assumption. See also Sandip's post [^]. I should admit I was very surprised by such a behaviour. :)

                                    If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                                    This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                                    [My articles]

                                    T Offline
                                    T Offline
                                    toxcct
                                    wrote on last edited by
                                    #18

                                    and what about {0} instead of {7}, and in Release Mode, not in Debug Mode ?

                                    [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

                                    CPalliniC D 2 Replies Last reply
                                    0
                                    • T toxcct

                                      and what about {0} instead of {7}, and in Release Mode, not in Debug Mode ?

                                      [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

                                      CPalliniC Offline
                                      CPalliniC Offline
                                      CPallini
                                      wrote on last edited by
                                      #19

                                      See my added remark here [^]. BTW my tests were of course performed both in Debug and the Release mode. ;P :)

                                      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                                      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                                      [My articles]

                                      In testa che avete, signor di Ceprano?

                                      1 Reply Last reply
                                      0
                                      • T toxcct

                                        and what about {0} instead of {7}, and in Release Mode, not in Debug Mode ?

                                        [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

                                        D Offline
                                        D Offline
                                        David Crow
                                        wrote on last edited by
                                        #20

                                        toxcct wrote:

                                        ...and what about {0} instead of {7}...

                                        0 works fine.

                                        toxcct wrote:

                                        ...and in Release Mode, not in Debug Mode ?

                                        Same results for both 0 and 7. This is why I only do it when setting things to 0. I use memset() otherwise.

                                        "Love people and use things, not love things and use people." - Unknown

                                        "The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch

                                        CPalliniC 1 Reply Last reply
                                        0
                                        • T toxcct

                                          SandipG :) wrote:

                                          Same output with Visual C++ 6.0

                                          Visual C++ is not what a decent programmer call a standard compliant compiler, Sir !

                                          [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

                                          CPalliniC Offline
                                          CPalliniC Offline
                                          CPallini
                                          wrote on last edited by
                                          #21

                                          IMHO such a discrepancy would be too gross, even for VC6. :)

                                          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                                          This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                                          [My articles]

                                          In testa che avete, signor di Ceprano?

                                          T 1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups