I'm not really into C design patterns, so my doubt is potentially simple (although a little specific). The real application of this question is harder to explain, so let me simplify it.
Suppose I have an array, in which I want to store prime numbers. The number of primes this array contains is defined by NUMBER_OF_PRIMES
, a constant defined at compile time.
Thus, we have:
unsigned primes[NUMBER_OF_PRIMES];
If the size was fixed, I could pre-compute the primes and initialize the array as usual:
unsigned primes[NUMBER_OF_PRIMES] = { 2, 3, 5, 7, ... };
But that would be rather ugly if NUMBER_OF_PRIMES
were, let's say, greater than 200. I want some way to run a function at runtime, but before main(), that will put those primes numbers there. Of course I could do:
unsigned* primes = make_primes(NUMBER_OF_PRIMES);
which would allocate the necessary memory and run before main. The main problem is: this array would be in a header file, but it's value would contain something that's hidden inside the corresponding .c file. what I thought I could do is:
/* Header file */
unsigned primes[NUMBER_OF_PRIMES];
/* C file */
int nothing = initialize_primes(); /* This function would write the
values to the array, using things that are not available in the
header (it is in the .c, so it can reference them), and return anything */
Another way is obviously putting initialize_primes()
in the header file, and asking the user to call it inside the main function (like some libraries' init()
function). However, I'd like not to force main()
to contain a call to this function.
My question is if there is any elegant way to do this that is universally accepted/used, or if this is ridiculous and I should ask main()
to call an init()
function to avoid unnecessary, obscure code.
As I said, I'm not working with anything related to primes; this is just an example with the same challenge.
Best regards.