Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 746
Mark A. Donohoe Avatar asked Feb 06 '12 04:02

Mark A. Donohoe


1 Answers

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.

like image 162
Seth Carnegie Avatar answered Nov 17 '22 09:11

Seth Carnegie