I saw the code below in this answer to What is your favorite C programming trick?
What does this piece of code do? How is it useful?
double normals[][] = {
#include "normals.txt"
};
I saw the code below in this answer to What is your favorite C programming trick?
What does this piece of code do? How is it useful?
double normals[][] = {
#include "normals.txt"
};
This uses the preprocessor to initialize an array.
It's basically inlining the code that populates the array with values, by reading it from an external text file. Note that this requires that "normals.txt" be filled with values that match the C syntax, ie:
{0, 0, 1},
{0, 1, 0},
{1, 0, 0}
The 2D array is initialised with the contents of the file "normals.txt", which presumably contains valid C code, and is probably shared with other code or is the output from other software.
Using the preprocessor directive #include
will paste in-place any code from the file it references ... thus whatever is inside of "normals.txt" would be text formatted as valid C-syntax code that fit in the array initializer list for normals
.
Since preprocessing takes place before the actual compilation step, this will create valid C-code to initialize the normals
2D array without the coder having to create a lot of text in the actual .c file that initializes the values of the array.
This makes for ugly code. A much cleaner option would be to have whatever is generating the normals.txt file to just generate a .c file that has the full array declaration.