You can use union.
struct mystruct_s { ... }; typedef union { struct mystruct_s s; unsigned char padding[512]; } mystruct;
This will provide a combination of 512 bytes or more. Then you can make sure that it is no more than 512 bytes using a static statement somewhere in your code:
char array[sizeof(mystruct) != 512 ? -1 : 1];
If you are using C11, there is a better way to do this. I donβt know anyone who uses C11 yet. The standard was published a few weeks ago.
_Static_assert(sizeof(mystruct) == 512, "mystruct must be 512 bytes");
Please note that the only way to fill with zeros is to set the zeros to manual ( calloc or memset ). The compiler ignores padding bytes.
source share