S
SerGioGio
Hello,
I want to build a ".lib" library where I will have some constants.
The constants are not only int, char, but also struct constants.
How do I declare / implement these struct constants? I came up with 3
solutions but I am really satisfied with none of them:
1. use #define
This can work with constants int or char*, but not really with structures.
If you have
| #define my_struct_constant { { 1, 2 }, 3 }
you can then write
| my_struct constant = my_struct_constant;
but subsequentely you cannot write
| constant = my_struct_constant;
2. declare the variable in .h:
| extern my_struct my_struct_constant;
and implement in a .c:
| my_struct my_struct_constant = { { 1, 2 }, 3 };
you can then write
| my_struct constant = my_struct_constant;
| constant = my_struct_constant;
So far so good.
But I understand however that the initialisation will not be performed...
So I will need to provide an init() function and require the users of the
..lib to call these function...
In addition I will need two lines in two separate files (declaration /
implementation) so it is harder to maintain.
3. provide a make function
| extern my_struct make_my_struct(int, int, int);
and use #define:
| #define my_struct_constant make_my_struct(1, 2, 3)
you can then write
| my_struct constant = my_struct_constant;
| constant = my_struct_constant;
But this does not scale very well, you need to provide make functions for
all the types you will have constants.
Are there any better options than thses ones?
Thanks in advance for your comments and suggestions!
SerGioGioGio
I want to build a ".lib" library where I will have some constants.
The constants are not only int, char, but also struct constants.
How do I declare / implement these struct constants? I came up with 3
solutions but I am really satisfied with none of them:
1. use #define
This can work with constants int or char*, but not really with structures.
If you have
| #define my_struct_constant { { 1, 2 }, 3 }
you can then write
| my_struct constant = my_struct_constant;
but subsequentely you cannot write
| constant = my_struct_constant;
2. declare the variable in .h:
| extern my_struct my_struct_constant;
and implement in a .c:
| my_struct my_struct_constant = { { 1, 2 }, 3 };
you can then write
| my_struct constant = my_struct_constant;
| constant = my_struct_constant;
So far so good.
But I understand however that the initialisation will not be performed...
So I will need to provide an init() function and require the users of the
..lib to call these function...
In addition I will need two lines in two separate files (declaration /
implementation) so it is harder to maintain.
3. provide a make function
| extern my_struct make_my_struct(int, int, int);
and use #define:
| #define my_struct_constant make_my_struct(1, 2, 3)
you can then write
| my_struct constant = my_struct_constant;
| constant = my_struct_constant;
But this does not scale very well, you need to provide make functions for
all the types you will have constants.
Are there any better options than thses ones?
Thanks in advance for your comments and suggestions!
SerGioGioGio