Been a loooooong time since I've actually coded straight c (not even C++ but c) and I know how to use the extern
keyword to share a variable between separate .c files, but what I can't remember is how to share constant data between files?
For example, say I have this... (note, this is not c code (or if it is, its an accident) but rather pseudo-code to show what I want):
const char const * WEEKDAYS[] = {
"Sunday",
"Monday",
"Tuesday"
}
Now I'm trying to create an array of char pointers that point to the data. Again, this is constant data so I'd like to just define it in a header directly, but that's where I can't figure out how to do it, or if that isn't how you should do it anyway and you should still declare it in the c file, then use extern in the header you include elsewhere.
Again, been a long time since I've had to deal with this thanks to the newer, more modern languages, but hoping you can help.
It's the same as for variables:
// header
extern const char * const WEEKDAYS[3];
// implementation
const char * const WEEKDAYS[3] = {
"Sunday",
"Monday",
"Tuesday"
};
Also you probably want const char * const
, not const char const *
which is invalid syntax.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With