How can I initialize an array of structs where the array size is set at compile time?
-
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
}; -
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
}; -
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
};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];
-
Please edit the above, and remove all that formatting. People here can read normal font sizes quite well.
-
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];
-
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).
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.
-
Then I know only four ways:
- Like you have done with multiple preprocessor
#if
statements. - Using weird preprocessor macros to create a loop.
- Initialising by code using a loop.
- 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.
- Like you have done with multiple preprocessor
-
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.
-
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
};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.