'Move large static array into a separate file: .c or .h?
I've got a large pre-calculated 4D array written in C:
static float coeffs[257][4][8][2] =
{
{
{ { +0.999778485478688f, +0.021047089441095f }, { +1.000000000000000f, -0.000000000000000f }, { +0.999778485478688f, -0.021047089441095f }, { +0.999556987314086f, -0.029762881439599f }, { +0.999778485478688f, -0.021047089441095f }, { +1.000000000000000f, +0.000000000000000f }, { +0.999778485478688f, +0.021047089441095f }, { +0.999556987314086f, +0.029762881439599f }, },
{ { +0.999778485478688f, +0.021047089441095f }, { +0.999556987314086f, +0.029762881439599f }, { +0.999778485478688f, +0.021047089441095f }, { +1.000000000000000f, +0.000000000000000f }, { +0.999778485478688f, -0.021047089441095f }, { +0.999556987314086f, -0.029762881439599f }, { +0.999778485478688f, -0.021047089441095f }, { +1.000000000000000f, -0.000000000000000f }, },
{ { +0.999778485478688f, -0.021047089441095f }, { +1.000000000000000f, +0.000000000000000f }, { +0.999778485478688f, +0.021047089441095f }, { +0.999556987314086f, +0.029762881439599f }, { +0.999778485478688f, +0.021047089441095f }, { +1.000000000000000f, +0.000000000000000f }, { +0.999778485478688f, -0.021047089441095f }, { +0.999556987314086f, -0.029762881439599f }, },
{ { +0.999778485478688f, -0.021047089441095f }, { +0.999556987314086f, -0.029762881439599f }, { +0.999778485478688f, -0.021047089441095f }, { +1.000000000000000f, +0.000000000000000f }, { +0.999778485478688f, +0.021047089441095f }, { +0.999556987314086f, +0.029762881439599f }, { +0.999778485478688f, +0.021047089441095f }, { +1.000000000000000f, +0.000000000000000f }, },
},
.
.
.
},
}
It's about 1500 lines, so I would like to move it into a separate file.
The array should be static as it is used only in a single C file.
The question is, should such static arrays be moved into a header file and then included as #include "coeffs.h", or should they be a separate C file included as #include "coeffs.c"? Does it matter?
Solution 1:[1]
I would not use a .h file nor a .c file.
The .h extension indicates that the file is intended for possible inclusion in several translation units. This is not the case.
The .c extension indicates that the file is the main file of a translation unit. Some IDEs will automatically build it separately if found within the project folder.
Hence, (almost) any extension which is not .h or .c would be better, e.g. .inc. The .c file needing coeffs would then include it as follows:
#include "coeffs.inc"
I would recommend putting the entire definition of coeffs in the file because the initializer list and the dimensions of the coeffs array are tightly coupled.
If coeffs is needed in several translation units, then of course it should be declared as extern in a .h file and the definition/initialization could then be in a separate .c file. However, in this case where coeffs is static (private within a single translation unit), the above approach is better so that it is clear that the array is local to the translation unit.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | nielsen |
