Reputation: 47
Below I have the code for a struct in a program I'm writing. I would like to be able to initialize these parameters in some sort of loop, but I can't seem to figure out how to configure the .water
/.air
/.purge
properties to update with the loop. nozzle_count
is 16 so I would like to find a way to achieve the code below without having to just copy and paste it 16 times. I'd appreciate any input. Thanks!
static nozzle nozzles[nozzle_count] = {
{
.water = C1_WATER, .air = C1_AIR, .purge = C1_PURGE,
.interval = 15*60*1000, .cycle = 0*3*5*1000, .purge_length = 0*4*1000,
.state = WAIT
},
{
.water = C2_WATER, .air = C2_AIR, .purge = C2_PURGE,
.interval = 2*60*1000, .cycle = 1*3*1000, .purge_length = 0*4*1000,
.state = WAIT
},
{
.water = C3_WATER, .air = C3_AIR, .purge = C3_PURGE,
.interval = 2*60*1000, .cycle = 0*1*5*1000, .purge_length = 0*4*1000,
.state = WAIT
}
{
.water = C4_WATER, .air = C4_AIR, .purge = C4_PURGE,
.interval = 2*60*1000, .cycle = 0*1*5*1000, .purge_length = 0*4*1000,
.state = WAIT
}
{
.water = C5_WATER, .air = C5_AIR, .purge = C5_PURGE,
.interval = 2*60*1000, .cycle = 0*1*5*1000, .purge_length = 0*4*1000,
.state = WAIT
}
};
Upvotes: 1
Views: 508
Reputation: 402
Can you show the definition of *_WATER
, *_AIR
, *_PURGE
?
for (i = 0; i < nozzle_count; i++)
{
nozzles[i] = (struct nozzle){WATER[i], AIR[i], PURGE[i], 15*60*1000, 0, 0, WAIT};
}
Upvotes: 0
Reputation: 9962
I think your code looks perfectly fine, even if the length of your array is increased from 5 to 16. Two possible suggestions.
If you remove the C99 initialization fluff, then everything can fit into a single line:
static nozzle nozzles[nozzle_count] = {
// water air purge interval cycle purge_length state
// -------- ------ -------- ---------- ---------- ------------ ----
{ C1_WATER, C1_AIR, C1_PURGE, 15*60*1000, 0*3*5*1000, 0*4*1000, WAIT },
// ...
};
This is a simple table, just 16 lines long, which is clear and obvious and which is easy-to-maintain.
Secondly, if you need to keep the per-member initialization, you could enclose it a macro, which helps with DRY:
#define FOO(a, b, c, d, e, f, g) \
{ \
.water = (a), .air = (b), .purge = (c), \
.interval = (d), .cycle = (e), .purge_length = (f), \
.state = (b) \
}
static nozzle nozzles[nozzle_count] = {
// water air purge interval cycle purge_length state
// -------- ------ -------- ---------- ---------- ------------ ----
FOO( C1_WATER, C1_AIR, C1_PURGE, 15*60*1000, 0*3*5*1000, 0*4*1000, WAIT ),
// ...
};
Upvotes: 1
Reputation: 67638
The only workaround I can think of is:
const TYPE_OF_CxPURGE PURGE[] = {...};
const TYPE_OF_CxAIR AIR[] = {....};
const TYPE_OF_CxWATER WATER[] = {....};
And somewhere in the code
for(size_t index = 0; index < sizeof(nozzles) / sizeof(nozzles[0]); index ++)
{
nozzles.water = WATER[index];
nozzles.air = AIR[index];
nozzles.purge = PURGE[index];
nozzles.interval = 15*60*1000;
nozzles.cycle = 0*3*5*1000;
nozzles.purge_length = 0*4*1000,
nozzles.state = WAIT
}
But I think that the normal initialization is better. Only 16 elements. If you have more just write the script to generate this initialization code
Upvotes: 1