1

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
    }
};
6
  • 1
    I do not believe what you are seeking is possible. Commented Jul 23, 2018 at 23:03
  • 1
    It's conceivably possible in C++ with constexpr and/or template magic, but yeah, in C you'd be limited to truly terrible recursive macros, where the cure is worse than the disease. Commented Jul 23, 2018 at 23:06
  • 1
    If some (or all) values are potentially different and irregular then there's no meaningful way to use a loop here. Why would you even want a loop? What would be the point? Commented Jul 23, 2018 at 23:13
  • 1
    interval and cycle appear to change. Is that by accident? If not, how do you derive those values? Commented Jul 23, 2018 at 23:14
  • 2
    Show the definitions of C1_WATER, C1_AIR, C2_WATER etc. Commented Jul 23, 2018 at 23:14

3 Answers 3

1

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 ),
//  ...
};
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

0

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};
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.