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?
CodePudding user response:
The static arrays contain definitions, so they need to be in a .c
file.
If you put them in .h
files, different translation units will define the same constants.
In your case, data
is static, so by no means it will be in a .h
file. It is not linked with anything externally, so no need to declare it in a header file.
If the static data is generated by some other program in a file "data", you can do so:
file.c:
static float coeffs[257][4][8][2] =
{
#include "data"
}
and generate "data" as a valid C-input.
CodePudding user response:
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.