Is there a way to use macro for padding inside structs, I mean like that:
struct Monument{
char name[32];
PADDING( 4 * sizeof(int));
int cityId;
PADDING( 4 * sizeof(int));
int age;
}
where PADDING(s) macro just adds space.
Is there a way to use macro for padding inside structs, I mean like that:
struct Monument{
char name[32];
PADDING( 4 * sizeof(int));
int cityId;
PADDING( 4 * sizeof(int));
int age;
}
where PADDING(s) macro just adds space.
You can define it like that:
#define TOKENPASTE(x, y) x ## y
#define TOKENPASTE2(x, y) TOKENPASTE(x, y)
#define PADDING(size) char TOKENPASTE2(padding_, __LINE__) [size]
Change field name prefix as you like it (preferably one that won't ever collide with other possible members).
Keep in mind (as mentioned by Remy Lebeau and François Andrieux in comments) that compiler can sometimes add unexpected padding. For reference you can read:
EDIT: Sorry, initial code was incorrect (it was incorrectly using __LINE__
expansion in macro). I fixed it using solution from: https://stackoverflow.com/a/1597129/1561140