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. Other Discussions
  3. The Weird and The Wonderful
  4. Initializing an array of structures

Initializing an array of structures

Scheduled Pinned Locked Moved The Weird and The Wonderful
data-structures
23 Posts 11 Posters 1 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.
  • D dighn

    A "good" way to pad your line count.

    P Online
    P Online
    PIEBALDconsult
    wrote on last edited by
    #9

    I could find better ways.

    L 1 Reply Last reply
    0
    • P PIEBALDconsult

      I could find better ways.

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #10

      glad to see you're back. :-D

      Luc Pattyn [Forum Guidelines] [My Articles]


      - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


      P 1 Reply Last reply
      0
      • L Luc Pattyn

        glad to see you're back. :-D

        Luc Pattyn [Forum Guidelines] [My Articles]


        - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


        P Online
        P Online
        PIEBALDconsult
        wrote on last edited by
        #11

        Was just in Vegas, couldn't be bothered to find a wifi hot spot.

        1 Reply Last reply
        0
        • A Adrian Cole

          struct _X
          {
          int field1;
          int field2;
          int field3;
          } anArray[15];

          for (int i = 0; i < 15; ++i)
          {
          anArray[i].field1 = 1;
          }

          for (int i = 0; i < 15; ++i)
          {
          anArray[i].field2 = 2;
          }

          for (int i = 0; i < 15; ++i)
          {
          anArray[i].field3 = 3;
          }

          M Offline
          M Offline
          Mladen Jankovic
          wrote on last edited by
          #12

          Really horrible, poor guy didn't consider performance of such code. I have far more superior solution:

          anArray[0].field1 = 1;
          anArray[1].field1 = 1;
          anArray[2].field1 = 1;
          anArray[3].field1 = 1;
          anArray[4].field1 = 1;
          anArray[5].field1 = 1;
          anArray[6].field1 = 1;
          anArray[7].field1 = 1;
          anArray[8].field1 = 1;
          anArray[9].field1 = 1;
          anArray[10].field1 = 1;
          anArray[11].field1 = 1;
          anArray[12].field1 = 1;
          anArray[13].field1 = 1;
          anArray[14].field1 = 1;

          anArray[0].field2 = 2;
          anArray[1].field2 = 2;
          anArray[2].field2 = 2;
          anArray[3].field2 = 2;
          anArray[4].field2 = 2;
          anArray[5].field2 = 2;
          anArray[6].field2 = 2;
          anArray[7].field2 = 2;
          anArray[8].field2 = 2;
          anArray[9].field2 = 2;
          anArray[10].field2 = 2;
          anArray[11].field2 = 2;
          anArray[12].field2 = 2;
          anArray[13].field2 = 2;
          anArray[14].field2 = 2;

          anArray[0].field3 = 3;
          anArray[1].field3 = 3;
          anArray[2].field3 = 3;
          anArray[3].field3 = 3;
          anArray[4].field3 = 3;
          anArray[5].field3 = 3;
          anArray[6].field3 = 3;
          anArray[7].field3 = 3;
          anArray[8].field3 = 3;
          anArray[9].field3 = 3;
          anArray[10].field3 = 3;
          anArray[11].field3 = 3;
          anArray[12].field3 = 3;
          anArray[13].field3 = 3;
          anArray[14].field3 = 3;

          [Genetic Algorithm Library]

          Y 1 Reply Last reply
          0
          • T Thomas Weller 0

            No matter what language - all of them allow at least for summarizing this in one loop construct... :) Regards Thomas

            www.thomas-weller.de Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.
            Programmer - an organism that turns coffee into software.

            F Offline
            F Offline
            Fatbuddha 1
            wrote on last edited by
            #13

            I don't know, maybe Brain fuck doesn't. :laugh: Cheers

            You have the thought that modern physics just relay on assumptions, that somehow depends on a smile of a cat, which isn’t there.( Albert Einstein)

            V 1 Reply Last reply
            0
            • M Mladen Jankovic

              Really horrible, poor guy didn't consider performance of such code. I have far more superior solution:

              anArray[0].field1 = 1;
              anArray[1].field1 = 1;
              anArray[2].field1 = 1;
              anArray[3].field1 = 1;
              anArray[4].field1 = 1;
              anArray[5].field1 = 1;
              anArray[6].field1 = 1;
              anArray[7].field1 = 1;
              anArray[8].field1 = 1;
              anArray[9].field1 = 1;
              anArray[10].field1 = 1;
              anArray[11].field1 = 1;
              anArray[12].field1 = 1;
              anArray[13].field1 = 1;
              anArray[14].field1 = 1;

              anArray[0].field2 = 2;
              anArray[1].field2 = 2;
              anArray[2].field2 = 2;
              anArray[3].field2 = 2;
              anArray[4].field2 = 2;
              anArray[5].field2 = 2;
              anArray[6].field2 = 2;
              anArray[7].field2 = 2;
              anArray[8].field2 = 2;
              anArray[9].field2 = 2;
              anArray[10].field2 = 2;
              anArray[11].field2 = 2;
              anArray[12].field2 = 2;
              anArray[13].field2 = 2;
              anArray[14].field2 = 2;

              anArray[0].field3 = 3;
              anArray[1].field3 = 3;
              anArray[2].field3 = 3;
              anArray[3].field3 = 3;
              anArray[4].field3 = 3;
              anArray[5].field3 = 3;
              anArray[6].field3 = 3;
              anArray[7].field3 = 3;
              anArray[8].field3 = 3;
              anArray[9].field3 = 3;
              anArray[10].field3 = 3;
              anArray[11].field3 = 3;
              anArray[12].field3 = 3;
              anArray[13].field3 = 3;
              anArray[14].field3 = 3;

              [Genetic Algorithm Library]

              Y Offline
              Y Offline
              Yusuf
              wrote on last edited by
              #14

              Do you get paid by LOC ;)

              Yusuf Oh didn't you notice, analogous to square roots, they recently introduced rectangular, circular, and diamond roots to determine the size of the corresponding shapes when given the area. Luc Pattyn[^]

              P 1 Reply Last reply
              0
              • F Fatbuddha 1

                I don't know, maybe Brain fuck doesn't. :laugh: Cheers

                You have the thought that modern physics just relay on assumptions, that somehow depends on a smile of a cat, which isn’t there.( Albert Einstein)

                V Offline
                V Offline
                VentsyV
                wrote on last edited by
                #15

                Fatbuddha 1 wrote:

                I don't know, maybe Brain f*** doesn't.

                I'm pretty sure it actually does.

                1 Reply Last reply
                0
                • L Luc Pattyn

                  some languages such as C# allow for code inside a struct; C does not. How would you do it in C? :)

                  Luc Pattyn [Forum Guidelines] [My Articles]


                  - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


                  B Offline
                  B Offline
                  bulg
                  wrote on last edited by
                  #16

                  modified on Thursday, March 26, 2009 1:17 PM

                  L B 2 Replies Last reply
                  0
                  • B bulg

                    modified on Thursday, March 26, 2009 1:17 PM

                    L Offline
                    L Offline
                    Luc Pattyn
                    wrote on last edited by
                    #17

                    that is something different, it generates no run-time code. It may or may not be sufficient for what the original program needed, assuming you duplicate the constants 15 times. :)

                    Luc Pattyn [Forum Guidelines] [My Articles]


                    - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


                    1 Reply Last reply
                    0
                    • B bulg

                      modified on Thursday, March 26, 2009 1:17 PM

                      B Offline
                      B Offline
                      bulg
                      wrote on last edited by
                      #18

                      Can't modify a deleted message :P I couldn't make it scalable so I deleted my post:

                      struct k[15] = { {1,2,3},...}

                      L 1 Reply Last reply
                      0
                      • B bulg

                        Can't modify a deleted message :P I couldn't make it scalable so I deleted my post:

                        struct k[15] = { {1,2,3},...}

                        L Offline
                        L Offline
                        Luc Pattyn
                        wrote on last edited by
                        #19

                        bulg wrote:

                        Can't modify a deleted message

                        No, however you can edit a message instead of deleting it. :)

                        Luc Pattyn [Forum Guidelines] [My Articles]


                        - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


                        1 Reply Last reply
                        0
                        • T Thomas Weller 0

                          No matter what language - all of them allow at least for summarizing this in one loop construct... :) Regards Thomas

                          www.thomas-weller.de Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.
                          Programmer - an organism that turns coffee into software.

                          P Offline
                          P Offline
                          peterchen
                          wrote on last edited by
                          #20

                          I especially love the brackets he put around the loop bodies - in case he needs to add some additional instructions there!

                          Burning Chrome ^ | Linkify!| FoldWithUs! | sighist

                          T 1 Reply Last reply
                          0
                          • P peterchen

                            I especially love the brackets he put around the loop bodies - in case he needs to add some additional instructions there!

                            Burning Chrome ^ | Linkify!| FoldWithUs! | sighist

                            T Offline
                            T Offline
                            Thomas Weller 0
                            wrote on last edited by
                            #21

                            peterchen wrote:

                            I especially love the brackets he put around the loop bodies

                            Well, this can be a coding guideline. It may look a bit silly and pedantic to put brackets around a single statement, because it makes you write things like:

                            if (somecondition)
                            {
                            return;
                            }

                            Tools like e.g. ReSharper (for C#) do that automatically, so you don't have to type. I've seen this often in larger teams, and it brings some more readability to the code. I do it myself... Or, as you suggest, it's simply an absence of any thinking or understanding. Who knows ? :cool: Regards Thomas

                            www.thomas-weller.de Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.
                            Programmer - an organism that turns coffee into software.

                            P 1 Reply Last reply
                            0
                            • T Thomas Weller 0

                              peterchen wrote:

                              I especially love the brackets he put around the loop bodies

                              Well, this can be a coding guideline. It may look a bit silly and pedantic to put brackets around a single statement, because it makes you write things like:

                              if (somecondition)
                              {
                              return;
                              }

                              Tools like e.g. ReSharper (for C#) do that automatically, so you don't have to type. I've seen this often in larger teams, and it brings some more readability to the code. I do it myself... Or, as you suggest, it's simply an absence of any thinking or understanding. Who knows ? :cool: Regards Thomas

                              www.thomas-weller.de Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.
                              Programmer - an organism that turns coffee into software.

                              P Offline
                              P Offline
                              peterchen
                              wrote on last edited by
                              #22

                              Thomas Weller wrote:

                              Well, this can be a coding guideline.

                              Excactly - even though I don't subscribe to it, it is a common guideline to avoid a mistake like this:

                              for (int i = 0; i < 15; ++i)
                              anArray[i].field1 = 1;
                              someotherarray[i].somefield = 2;

                              It's just a strange idea, but someone, in the future, MIGHT come up with maybe another array to be assigned a different value. :rolleyes:

                              Don't attribute to stupidity what can be equally well explained by buerocracy.
                              My latest article | Linkify!| FoldWithUs! | sighist

                              1 Reply Last reply
                              0
                              • Y Yusuf

                                Do you get paid by LOC ;)

                                Yusuf Oh didn't you notice, analogous to square roots, they recently introduced rectangular, circular, and diamond roots to determine the size of the corresponding shapes when given the area. Luc Pattyn[^]

                                P Offline
                                P Offline
                                peterchen
                                wrote on last edited by
                                #23

                                That would go

                                // Set field to 1:
                                anArray
                                [
                                // index
                                0
                                ].
                                // field name
                                field1

                                // value
                                1;

                                Don't attribute to stupidity what can be equally well explained by buerocracy.
                                My latest article | Linkify!| FoldWithUs! | sighist

                                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