r/C_Programming • u/heavymetalmixer • Sep 11 '26
Question Unmodifiable globals initialized from functions?
Right now I'm trying to use Windows's SYSTEM_INFO to initialize 2 global constants, but global constants can only be initialized with constant expressions so this cannot be done.
Is there a way I can have "read-only" global variables that can be initialized from functions?
6
Upvotes
View all comments
Show parent comment
2
u/Wertbon1789 Sep 11 '26
Yeah, pretty much. You can't just cast away the const-ness, because if the global is a constant expression, it probably will be put into .rodata.
One thing that came to my mind would be putting the global into another translation unit where it's not const, and referencing it with an
extern const <something>, but I don't know if that would work, or violate anything.