Besides macros (which have their downsides), unnamed enumerations are often used for the purpose to just provide compile-time constants. This would change your code like this.
typedef struct {
const int x;
const int y;
} my_struct;
enum {
a = 8,
b = 12,
};
my_struct test = { a, b };
... also expressions involving compile-time constants can be compile-time constants themselves ...
my_struct test1 = { a+1, b };
my_struct test2 = { a, 'H' };
Also the sizeof
operator can produce compile-time constants:
my_struct test3 = { sizeof(my_struct), b };
Even if this example looks not very useful, it compiles.
For learning what exactly constant expressions are, see for example Constant expressions - despite the URL (cppreference.com
), this is the C reference part.