How do you share a constant array of strings between files?

There was a loooooong time, since I actually encoded directly c (even C ++, but c), and I know how to use the extern keyword to exchange a variable between individual .c files, but that I cannot remember how share persistent data between files?

For example, say I have this ... (notice, this is not c-code (or if so, its an accident), but rather a 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 data. Again, this is persistent data, so I would just like to define it in the header directly, but where I canโ€™t figure out how to do this, or if itโ€™s not how you should do it, and you should still declare it in the c file, then use extern in the header that you included elsewhere.

Again, I havenโ€™t done this for a long time thanks to new, more modern languages, but I hope you can help.

+4
source share
1 answer

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 not valid syntax.

+5
source

Source: https://habr.com/ru/post/1394949/


All Articles