0

This is purely for readability. I want to have a very large constant array of values, but it makes my file a lot less pleasant to read. So I'd like to have that array initialized after use, though I want it to be constant. I know this is probably impossible, but it's probably also very common. So what workarounds are there ? I do not want to create a separate file right now.

Some thing like :

static const float array_of_values[1000];

float getValueFromArray(int index)
{
    return array_of_values[index];
}

array_of_values = {0.123f, 0.456f, 0.789f ...};
Charles
  • 988
  • 1
  • 11
  • 28
  • 5
    Why don't you want a separate file? `#include`-ing a second file is a nice common solution. – John Kugelman May 11 '22 at 12:51
  • 2
    "I'd like to have that array initialized after use" That sentence doesn't make any sense. Please note that initialization and assignment are different terms in pretty much every programming language, including C (89). – Lundin May 11 '22 at 12:52
  • Also note that *all* use of the array will be after definition and initialization. The array would be initialized when the program is loaded, and any use of it will be after that. It doesn't matter if you define the function before or after you use it in a function, as long as it has been *declared*. – Some programmer dude May 11 '22 at 12:52

3 Answers3

1

First of all what you're doing is not initialization, it's plain assignment. And you can't assign to an array. And you can't have general statements outside of functions. If you want to initialize the array, you need to do it when defining the array.

With that said, you have to remember (or learn) that any definition without explicit initialization is tentative.

That means you can create a tentative definition, basically to get the declaration out of the way. Then at a later place in the source file you can add the actual definition:

static const float array_of_values[1000];

float getValueFromArray(int index)
{
    return array_of_values[index];
}

static const float array_of_values[] = { 0.123f, 0.456f, 0.789f, /* ... */ };
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • I think the first paragraph is irrelevant: the asker knows the code won't work, and is simply showing how they would like the code to look. – user253751 May 11 '22 at 13:39
  • One is never finished learning about C it seems ... thanks for this great info ! – Charles May 12 '22 at 09:40
1

These are common solutions to make the file less cumbersome to read:

static const float array_of_values[1000] = { MACRO }; // macro being an initalizer list

or

static const float array_of_values[1000] = 
{ 
  #include "initlist.h"
};

I would personally recommend the macro version since it's more flexible and less mysterious. You could still declare the macro in a separate header file.

There's also tentative definitions which is generally a bad idea, again because it makes the code mysterious and hard to read:

static const float array_of_values[1000];

float getValueFromArray(int index)
{
    return array_of_values[index];
}

static const float array_of_values[1000] = {0.123f, 0.456f, 0.789f};

#include <stdio.h>

int main (void)
{
  printf("%f\n", array_of_values[0]);
  printf("%f\n", getValueFromArray(0));
}
Lundin
  • 195,001
  • 40
  • 254
  • 396
  • Please expand on how tentative declarations are worse (more mysterious, harder to read - to quote your criteria) than those other solutions. They all seem mysterious and hard to read to me. – Charles May 12 '22 at 09:39
  • @Charles I guess it depends on one's preferences so it's a bit subjective. At least I find it strange to see the same variable definition at multiple places inside the same translation unit, but then I never use this language feature. Whereas `array_of_values[1000] = { INITIALIZER_LIST };` or perhaps `array_of_values[1000] = INITIALIZER_LIST;` ought to be pretty self-explanatory. – Lundin May 12 '22 at 09:42
  • Also there's actually a 4th version here: wrap the whole array in a struct then use a compound literal `the_struct = (the_struct_t){ .array = {0.123f, 0.456f, 0.789f} }`. The only benefit from this is that it's read/write in run-time rather than read-only. – Lundin May 12 '22 at 09:45
-3

Try this:

#include <stdio.h>

static float array_of_values_base[1000]; // the effective array has "base" in its identifier
static const float *array_of_values = array_of_values_base; // your array is now a pointer

float getValueFromArray(int index)
{
    return array_of_values[index];
}

int main(void) {
    array_of_values_base[0] = 0.123f;
    array_of_values_base[1] = 0.456f;
    array_of_values_base[2] = 0.789f;
    // ...
    printf("value at index 1 is %f\n", getValueFromArray(1));
}
pmg
  • 106,608
  • 13
  • 126
  • 198