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. How can I initialize an array of structs where the array size is set at compile time?

How can I initialize an array of structs where the array size is set at compile time?

Scheduled Pinned Locked Moved C / C++ / MFC
questiondata-structures
9 Posts 4 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.
  • A Offline
    A Offline
    arnold_w
    wrote on last edited by
    #1

    How can I do this in a smarter way in regular C-programming, without having an init function that initializes the values?

    struct myStruct_s {
    int myInt;
    Bool_t myBool;
    EventCallback_t myEventCallback;
    };

    #define DESIRED_ARRAY_SIZE (5)

    static struct myStruct_s myArray[] = {
    #if 1 <= DESIRED_ARRAY_SIZE
    {0xABCD, TRUE, NULL},
    #endif
    #if 2 <= DESIRED_ARRAY_SIZE
    {0xABCD, TRUE, NULL},
    #endif
    #if 3 <= DESIRED_ARRAY_SIZE
    {0xABCD, TRUE, NULL},
    #endif
    #if 4 <= DESIRED_ARRAY_SIZE
    {0xABCD, TRUE, NULL},
    #endif
    #if 5 <= DESIRED_ARRAY_SIZE
    {0xABCD, TRUE, NULL},
    #endif
    #if 6 <= DESIRED_ARRAY_SIZE
    {0xABCD, TRUE, NULL},
    #endif
    #if 7 <= DESIRED_ARRAY_SIZE
    {0xABCD, TRUE, NULL},
    #endif
    #if 8 <= DESIRED_ARRAY_SIZE
    {0xABCD, TRUE, NULL},
    #endif
    #if 9 <= DESIRED_ARRAY_SIZE
    {0xABCD, TRUE, NULL},
    #endif
    #if 10 <= DESIRED_ARRAY_SIZE
    {0xABCD, TRUE, NULL},
    #endif
    #if 11 <= DESIRED_ARRAY_SIZE
    {0xABCD, TRUE, NULL},
    #endif
    #if 12 <= DESIRED_ARRAY_SIZE
    {0xABCD, TRUE, NULL},
    #endif
    #if 13 <= DESIRED_ARRAY_SIZE
    {0xABCD, TRUE, NULL},
    #endif
    #if 14 <= DESIRED_ARRAY_SIZE
    {0xABCD, TRUE, NULL},
    #endif
    #if 15 <= DESIRED_ARRAY_SIZE
    {0xABCD, TRUE, NULL},
    #endif
    #if 16 <= DESIRED_ARRAY_SIZE
    {0xABCD, TRUE, NULL},
    #endif
    #if 17 <= DESIRED_ARRAY_SIZE
    {0xABCD, TRUE, NULL},
    #endif
    #if 18 <= DESIRED_ARRAY_SIZE
    {0xABCD, TRUE, NULL},
    #endif
    #if 19 <= DESIRED_ARRAY_SIZE
    {0xABCD, TRUE, NULL},
    #endif
    #if 20 <= DESIRED_ARRAY_SIZE
    {0xABCD, TRUE, NULL},
    #endif
    };

    L J J 3 Replies Last reply
    0
    • A arnold_w

      How can I do this in a smarter way in regular C-programming, without having an init function that initializes the values?

      struct myStruct_s {
      int myInt;
      Bool_t myBool;
      EventCallback_t myEventCallback;
      };

      #define DESIRED_ARRAY_SIZE (5)

      static struct myStruct_s myArray[] = {
      #if 1 <= DESIRED_ARRAY_SIZE
      {0xABCD, TRUE, NULL},
      #endif
      #if 2 <= DESIRED_ARRAY_SIZE
      {0xABCD, TRUE, NULL},
      #endif
      #if 3 <= DESIRED_ARRAY_SIZE
      {0xABCD, TRUE, NULL},
      #endif
      #if 4 <= DESIRED_ARRAY_SIZE
      {0xABCD, TRUE, NULL},
      #endif
      #if 5 <= DESIRED_ARRAY_SIZE
      {0xABCD, TRUE, NULL},
      #endif
      #if 6 <= DESIRED_ARRAY_SIZE
      {0xABCD, TRUE, NULL},
      #endif
      #if 7 <= DESIRED_ARRAY_SIZE
      {0xABCD, TRUE, NULL},
      #endif
      #if 8 <= DESIRED_ARRAY_SIZE
      {0xABCD, TRUE, NULL},
      #endif
      #if 9 <= DESIRED_ARRAY_SIZE
      {0xABCD, TRUE, NULL},
      #endif
      #if 10 <= DESIRED_ARRAY_SIZE
      {0xABCD, TRUE, NULL},
      #endif
      #if 11 <= DESIRED_ARRAY_SIZE
      {0xABCD, TRUE, NULL},
      #endif
      #if 12 <= DESIRED_ARRAY_SIZE
      {0xABCD, TRUE, NULL},
      #endif
      #if 13 <= DESIRED_ARRAY_SIZE
      {0xABCD, TRUE, NULL},
      #endif
      #if 14 <= DESIRED_ARRAY_SIZE
      {0xABCD, TRUE, NULL},
      #endif
      #if 15 <= DESIRED_ARRAY_SIZE
      {0xABCD, TRUE, NULL},
      #endif
      #if 16 <= DESIRED_ARRAY_SIZE
      {0xABCD, TRUE, NULL},
      #endif
      #if 17 <= DESIRED_ARRAY_SIZE
      {0xABCD, TRUE, NULL},
      #endif
      #if 18 <= DESIRED_ARRAY_SIZE
      {0xABCD, TRUE, NULL},
      #endif
      #if 19 <= DESIRED_ARRAY_SIZE
      {0xABCD, TRUE, NULL},
      #endif
      #if 20 <= DESIRED_ARRAY_SIZE
      {0xABCD, TRUE, NULL},
      #endif
      };

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Please edit the above, and remove all that formatting. People here can read normal font sizes quite well.

      A 1 Reply Last reply
      0
      • A arnold_w

        How can I do this in a smarter way in regular C-programming, without having an init function that initializes the values?

        struct myStruct_s {
        int myInt;
        Bool_t myBool;
        EventCallback_t myEventCallback;
        };

        #define DESIRED_ARRAY_SIZE (5)

        static struct myStruct_s myArray[] = {
        #if 1 <= DESIRED_ARRAY_SIZE
        {0xABCD, TRUE, NULL},
        #endif
        #if 2 <= DESIRED_ARRAY_SIZE
        {0xABCD, TRUE, NULL},
        #endif
        #if 3 <= DESIRED_ARRAY_SIZE
        {0xABCD, TRUE, NULL},
        #endif
        #if 4 <= DESIRED_ARRAY_SIZE
        {0xABCD, TRUE, NULL},
        #endif
        #if 5 <= DESIRED_ARRAY_SIZE
        {0xABCD, TRUE, NULL},
        #endif
        #if 6 <= DESIRED_ARRAY_SIZE
        {0xABCD, TRUE, NULL},
        #endif
        #if 7 <= DESIRED_ARRAY_SIZE
        {0xABCD, TRUE, NULL},
        #endif
        #if 8 <= DESIRED_ARRAY_SIZE
        {0xABCD, TRUE, NULL},
        #endif
        #if 9 <= DESIRED_ARRAY_SIZE
        {0xABCD, TRUE, NULL},
        #endif
        #if 10 <= DESIRED_ARRAY_SIZE
        {0xABCD, TRUE, NULL},
        #endif
        #if 11 <= DESIRED_ARRAY_SIZE
        {0xABCD, TRUE, NULL},
        #endif
        #if 12 <= DESIRED_ARRAY_SIZE
        {0xABCD, TRUE, NULL},
        #endif
        #if 13 <= DESIRED_ARRAY_SIZE
        {0xABCD, TRUE, NULL},
        #endif
        #if 14 <= DESIRED_ARRAY_SIZE
        {0xABCD, TRUE, NULL},
        #endif
        #if 15 <= DESIRED_ARRAY_SIZE
        {0xABCD, TRUE, NULL},
        #endif
        #if 16 <= DESIRED_ARRAY_SIZE
        {0xABCD, TRUE, NULL},
        #endif
        #if 17 <= DESIRED_ARRAY_SIZE
        {0xABCD, TRUE, NULL},
        #endif
        #if 18 <= DESIRED_ARRAY_SIZE
        {0xABCD, TRUE, NULL},
        #endif
        #if 19 <= DESIRED_ARRAY_SIZE
        {0xABCD, TRUE, NULL},
        #endif
        #if 20 <= DESIRED_ARRAY_SIZE
        {0xABCD, TRUE, NULL},
        #endif
        };

        J Offline
        J Offline
        Jochen Arndt
        wrote on last edited by
        #3

        When using C++ and all members should be initialised with the same value you can provide a constructor:

        struct myStruct_s {
        int myInt;
        Bool_t myBool;
        EventCallback_t myEventCallback;
        myStruct_s() : myInt(0xABCD), myBool(FALSE), myEventCallback(NULL) { }
        };

        static struct myStruct_s myArray[DESIRED_ARRAY_SIZE];

        A 1 Reply Last reply
        0
        • L Lost User

          Please edit the above, and remove all that formatting. People here can read normal font sizes quite well.

          A Offline
          A Offline
          arnold_w
          wrote on last edited by
          #4

          I didn't use any formatting. The forum has a bug, it does weird things to the formatting if you start a line with either #define of #if (try for yourself).

          J 1 Reply Last reply
          0
          • J Jochen Arndt

            When using C++ and all members should be initialised with the same value you can provide a constructor:

            struct myStruct_s {
            int myInt;
            Bool_t myBool;
            EventCallback_t myEventCallback;
            myStruct_s() : myInt(0xABCD), myBool(FALSE), myEventCallback(NULL) { }
            };

            static struct myStruct_s myArray[DESIRED_ARRAY_SIZE];

            A Offline
            A Offline
            arnold_w
            wrote on last edited by
            #5

            I forgot to mention, I'm programming in regular C.

            J 1 Reply Last reply
            0
            • A arnold_w

              I didn't use any formatting. The forum has a bug, it does weird things to the formatting if you start a line with either #define of #if (try for yourself).

              J Offline
              J Offline
              Jochen Arndt
              wrote on last edited by
              #6

              If you know HTML and want to insert HTML tags manually when posting (besides using the options on top of the editor window) have a look at the bottom of the page when posting a message. There are two options: "Treat my content as plain text, not as HTML" and "Use Markdown formatting" When both are unchecked nothing will be auto formatted. The first option can be also changed on your profile settings page in the Forums tab.

              A 1 Reply Last reply
              0
              • A arnold_w

                I forgot to mention, I'm programming in regular C.

                J Offline
                J Offline
                Jochen Arndt
                wrote on last edited by
                #7

                Then I know only four ways:

                1. Like you have done with multiple preprocessor #if statements.
                2. Using weird preprocessor macros to create a loop.
                3. Initialising by code using a loop.
                4. Writing a helper program that creates a file with the relevant code lines that can then be included in a source file or even linked when declaring the struct as extern. The helper program must then be executed by the make file (or a corresponding custom build option when using an IDE) before compiling.
                1 Reply Last reply
                0
                • J Jochen Arndt

                  If you know HTML and want to insert HTML tags manually when posting (besides using the options on top of the editor window) have a look at the bottom of the page when posting a message. There are two options: "Treat my content as plain text, not as HTML" and "Use Markdown formatting" When both are unchecked nothing will be auto formatted. The first option can be also changed on your profile settings page in the Forums tab.

                  A Offline
                  A Offline
                  arnold_w
                  wrote on last edited by
                  #8

                  Thanks.

                  1 Reply Last reply
                  0
                  • A arnold_w

                    How can I do this in a smarter way in regular C-programming, without having an init function that initializes the values?

                    struct myStruct_s {
                    int myInt;
                    Bool_t myBool;
                    EventCallback_t myEventCallback;
                    };

                    #define DESIRED_ARRAY_SIZE (5)

                    static struct myStruct_s myArray[] = {
                    #if 1 <= DESIRED_ARRAY_SIZE
                    {0xABCD, TRUE, NULL},
                    #endif
                    #if 2 <= DESIRED_ARRAY_SIZE
                    {0xABCD, TRUE, NULL},
                    #endif
                    #if 3 <= DESIRED_ARRAY_SIZE
                    {0xABCD, TRUE, NULL},
                    #endif
                    #if 4 <= DESIRED_ARRAY_SIZE
                    {0xABCD, TRUE, NULL},
                    #endif
                    #if 5 <= DESIRED_ARRAY_SIZE
                    {0xABCD, TRUE, NULL},
                    #endif
                    #if 6 <= DESIRED_ARRAY_SIZE
                    {0xABCD, TRUE, NULL},
                    #endif
                    #if 7 <= DESIRED_ARRAY_SIZE
                    {0xABCD, TRUE, NULL},
                    #endif
                    #if 8 <= DESIRED_ARRAY_SIZE
                    {0xABCD, TRUE, NULL},
                    #endif
                    #if 9 <= DESIRED_ARRAY_SIZE
                    {0xABCD, TRUE, NULL},
                    #endif
                    #if 10 <= DESIRED_ARRAY_SIZE
                    {0xABCD, TRUE, NULL},
                    #endif
                    #if 11 <= DESIRED_ARRAY_SIZE
                    {0xABCD, TRUE, NULL},
                    #endif
                    #if 12 <= DESIRED_ARRAY_SIZE
                    {0xABCD, TRUE, NULL},
                    #endif
                    #if 13 <= DESIRED_ARRAY_SIZE
                    {0xABCD, TRUE, NULL},
                    #endif
                    #if 14 <= DESIRED_ARRAY_SIZE
                    {0xABCD, TRUE, NULL},
                    #endif
                    #if 15 <= DESIRED_ARRAY_SIZE
                    {0xABCD, TRUE, NULL},
                    #endif
                    #if 16 <= DESIRED_ARRAY_SIZE
                    {0xABCD, TRUE, NULL},
                    #endif
                    #if 17 <= DESIRED_ARRAY_SIZE
                    {0xABCD, TRUE, NULL},
                    #endif
                    #if 18 <= DESIRED_ARRAY_SIZE
                    {0xABCD, TRUE, NULL},
                    #endif
                    #if 19 <= DESIRED_ARRAY_SIZE
                    {0xABCD, TRUE, NULL},
                    #endif
                    #if 20 <= DESIRED_ARRAY_SIZE
                    {0xABCD, TRUE, NULL},
                    #endif
                    };

                    J Offline
                    J Offline
                    Joe Woodbury
                    wrote on last edited by
                    #9

                    One approach is to not bother; just initialize the array to what the maximum size could be and then store the actual size in a static variable. If this isn't time critical, just do it dynamically. Or, make sure the default values is zero and do a memset early on. In this case, they're all the same, so doing a quick loop early on solves the problem.

                    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