I have structure
struct ABC {
int a;
int b;
}
and array of it as
struct ABC xyz[100];
I want to initialize it a = 10 and b = 20; for all array element.
Which is better way ?
I have structure
struct ABC {
int a;
int b;
}
and array of it as
struct ABC xyz[100];
I want to initialize it a = 10 and b = 20; for all array element.
Which is better way ?
While there is no particularly elegant way to initialize a big array like this in C, it is possible. You do not have to do it in runtime, as some answers falsely claim. And you don't want to do it in runtime, suppose the array is const
?
The way I do it is by defining a number of macros:
struct ABC {
int a;
int b;
};
#define ABC_INIT_100 ABC_INIT_50 ABC_INIT_50
#define ABC_INIT_50 ABC_INIT_10 ABC_INIT_10 ABC_INIT_10 ABC_INIT_10 ABC_INIT_10
#define ABC_INIT_10 ABC_INIT_2 ABC_INIT_2 ABC_INIT_2 ABC_INIT_2 ABC_INIT_2
#define ABC_INIT_2 ABC_INIT_1 ABC_INIT_1
#define ABC_INIT_1 {10, 20},
int main()
{
struct ABC xyz[100] =
{
ABC_INIT_100
};
}
Note that macros like these can be combined in any way, to make any number of initializations. For example:
#define ABC_INIT_152 ABC_INIT_100 ABC_INIT_50 ABC_INIT_2
With GCC you can use its extended syntax and do:
struct ABC xyz[100] = { [0 ... 99].a = 10, [0 ... 99].b = 20 };
For a portable solution I'd probably initialize one instance, and use a loop to copy that instance to the rest:
struct ABC xyz[100] = { [0].a = 10, [0].b = 20 };
for(size_t i = 1; i < sizeof xyz / sizeof *xyz; ++i)
xyz[i] = xyz[0];
This is somewhat cleaner to me than having the actual values in the loop. It can be said to express the desired outcome at a slightly higher level.
The above syntax ([0].a
and [0].b
) is not an extension, it's typical C99.
const
? –
Boabdil const
, without a honking big explicit init, or macros (like your answer). –
Kurgan const struct
each set to the exact same value in your EEPROM... –
Beauvoir const
? There is some redundancy to having more than one copy of read-only data, that could/should be factored out, perhaps. –
Kurgan for(unsigned int i=0; i<100; i++)
{
xyz[i].a = 10;
xyz[i].b = 20;
}
const
? –
Boabdil There's no explicit language support for initializing all the elements in an array of substructures to specific, non-zero default values, in the the way there is for initializing all elements to zero; you either have to initialize each element explicitly in the source at compile-time or you have to write a for() loop and initialize each element at startup.
As user @lundin points out in another answer, you can use preprocessor macros to reduce the typing involved in explicitly initializing those values, but as far as the C compiler is concerned, you're still initializing each element explicitly.
I'm not a c expert (so when you come for my blood...)
This complies and works fine!
typedef struct
{
double var1;
double var2;
double var3;
double var4;
}VectorData;
#define MAX_INDEX 100
VectorData Data[] = {[0 ... MAX_INDEX] = {0.0, 0.0, 0.0, 0.0}};
This allows you to imply the array size from MAX_INDEX. In this case you have an array with 101 elements.
© 2022 - 2024 — McMap. All rights reserved.
struct ABC xyz[100] = {[0 ... 99] = {10, 20}};
– Mohur