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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  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 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
    • CPalliniC CPallini

      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]

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

      yes, probably, but it's not *THE* compiler to test the standard ! ;) ;P

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

      1 Reply Last reply
      0
      • D David Crow

        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 Offline
        CPalliniC Offline
        CPallini
        wrote on last edited by
        #23

        DavidCrow wrote:

        0 works fine.

        It is just a side-effect of default (int) initialization: you're actually initializing only the first array item. Try the following code:

        #include <iostream>
        using namespace std;

        struct MyStruct
        {
        MyStruct():_i(-1), _j(0),_k(-1){ }
        MyStruct(int a):_i(a), _j(a), _k(a){ }
        int _i,_j,_k;
        };

        void main()
        {
        int i;
        MyStruct a[5] = {0};
        for (i=0; i<5; i++)
        {
        cout << i << ") {" << a[i]._i << ", " << a[i]._j <<", " << a[i]._k << "}" << endl;
        }
        }

        DavidCrow wrote:

        This is why I only do it when setting things to 0. I use memset() otherwise.

        The above is a wise approach. :)

        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

          DavidCrow wrote:

          0 works fine.

          It is just a side-effect of default (int) initialization: you're actually initializing only the first array item. Try the following code:

          #include <iostream>
          using namespace std;

          struct MyStruct
          {
          MyStruct():_i(-1), _j(0),_k(-1){ }
          MyStruct(int a):_i(a), _j(a), _k(a){ }
          int _i,_j,_k;
          };

          void main()
          {
          int i;
          MyStruct a[5] = {0};
          for (i=0; i<5; i++)
          {
          cout << i << ") {" << a[i]._i << ", " << a[i]._j <<", " << a[i]._k << "}" << endl;
          }
          }

          DavidCrow wrote:

          This is why I only do it when setting things to 0. I use memset() otherwise.

          The above is a wise approach. :)

          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
          #24

          For structs, I'd use memset(). I only use 0 for POD types.

          "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

            For structs, I'd use memset(). I only use 0 for POD types.

            "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
            #25

            DavidCrow wrote:

            For structs, I'd use memset(). I only use 0 for POD types.

            That's good. :)

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