--- In [email protected], "bobtransformer" <bobtransfor...@...> wrote:
>
> typedef  struct  _modbus_cmd_rd_hreg_t
> {
>     uint16_t start_addr;
>     uint16_t num_reg;
> }  modbus_cmd_rd_hreg_t;
> 
>  modbus_cmd_rd_hreg_t   holding_cmd =
>     {
>         4000,     //starting register
>         11       //Number of registers to read (Was 10)
>     };

If your compiler supports C99 designated initializers, and if allowed by your 
coding standards, you could also use:

  modbus_cmd_rd_hreg_t holding_cmd =
  {
    .start_addr = 4000,
    .num_reg = 11
  };

This has the advantage of making the code independent of the order in which the 
fields are declared in the typedef, and self-documenting to a certain extent. 
That is, in your original code, if the typedef was changed to:

typedef  struct  _modbus_cmd_rd_hreg_t
{
    uint16_t num_reg;
    uint16_t start_addr;
} modbus_cmd_rd_hreg_t;

but your initialization statement was overlooked, the code would still compile, 
but (presumably) it would no longer do what it was supposed to do.

Reply via email to