In my C program, I have an integer array of length 120. Initializing the array is complex enough that it needs its own function, however once it is initialized it never needs to be changed again. I want to initialize the array once upon starting the program, and from that point on I want it to be read-only. There are only two functions that access this array: One initializes it upon bootup (I will call this init()). The other reads it (I will call it eval()). init and eval are in the same file. How can I do what I am trying to do?
-I think I need the variable to be "extern", because it is accessed by two functions. However, extern variables cannot be initialized inside of a function like I need, and it is too complex to initialize in one line when I declare it. I can't initialize it inside of eval because eval runs half a million times per second and it will slow it down significantly.
-I think I need the variable to be "const", because the value isn't changed after initialization.
-So I declare the array as "const extern int xyz[120]" in my header.h file. Then I run init() early on in my main function. Compiler says "undefined reference to xyz", probably because I am trying to initialize an extern variable inside of a function.
-I don't understand the difference between the static keyword and the extern keyword. Should I be using static instead?
eval()won't modify the array. The alternatives are much more complex. You say the initialization must be done inside a function — is that because you need to make function calls or something? If you've got the space to spare, you could have theinit()function initialize a local zrray and then copy the result to the filestaticarray that is used byeval().