Converting data to a C header as way to store as binary-Collection of common programming errors
Your thinking is absolutely correct: you can use compiler’s help in converting textual representation to binary. Rather than using headers, I would put the data in a separate translation unit, and keep a fixed header with forward declarations of the data structures populated by your script:
Header:
// This is fixed
extern float data_array[];
extern size_t data_array_cnt;
CPP file:
// This gets generated by a script
float data_array[] = {1.2, 3.4, 5.6, 7.89 };
size_t data_array_cnt = sizeof(data_array)/sizeof(float);
The biggest difference between the two approaches is that keeping binary representation in a file lets you modify whatever is represented after you have compiled everything. In fact, you could swap in another binary in production, and it would take effect immediately. In contrast, the compiler route forces you to recompile your program every time your binary data needs to change: effectively, your binary data becomes “baked into” your program’s content.
In environments that support dynamic linking you can make a middle ground solution by isolating the binary data in a separate dynamically linked library, and compiling that library separately from your “main” code. The binary data remains part of code, but now you can swap in a new piece of data independently of the rest of your program.