Home > front end >  How do I add two 'unused fields in a structure in c?
How do I add two 'unused fields in a structure in c?

Time:10-07

I want to send 8 bytes structure and since I dont have any valid member I just wanna add two unused fields which are of 4 and 2 bytes .

My code below is throwing up an error duplicate member 'unused'

typedef struct
{
  uint16_t    leakRatemTm;
  uint16_t    unused;
  uint32_t    unused;
} stuct_t;

How do I actually add multiple unused fields in a structure.

Thanks

CodePudding user response:

You can use an array of char to pad with how many bytes you want. Use this in combination with the attribute packed:

typedef struct __attribute__((packed)); 
{
  uint16_t    leakRatemTm;
  uint8_t     unused[6]
} stuct_t;

If you need gaps you can do something like this:

typedef struct __attribute__((packed)); 
{
  uint16_t    leakRatemTm;
  uint8_t     unused1[1]; //1byte gap
  uint16_t    otherVar;
  uint8_t     unused2[3]; //Fill till 8bytes
} stuct_t;
  • Related