C
Christopher Collins
When declaring an array with file scope, you can specify its initial
contents, e.g.,
int data[6] = { 100, 200, 300, 400, 500, 600 };
Rather than initalize the full array at its point of declaration, I
would like to declare it in one source file, and initialize different
parts of it throughout some other source files. Below is an example
of what I'd like to do. This is not proper C syntax, but hopefullly
the meaning is clear.
/* File: a.c */
int data[6];
/* File: b.c */
extern int data[6];
data[0, 1, 2] = { 100, 200, 300 }; /* (file scope) */
/* File: c.c */
extern int data[6];
data[3, 4, 5] = { 400, 500, 600 }; /* (file scope) */
One obvious way to achieve this is to define an initialization
function in each of b.c and c.c, and call them at the start of the
program. However, I would prefer the array to get initialized
automatically.
Is there any way to do this? Any thoughts are much appreciated.
contents, e.g.,
int data[6] = { 100, 200, 300, 400, 500, 600 };
Rather than initalize the full array at its point of declaration, I
would like to declare it in one source file, and initialize different
parts of it throughout some other source files. Below is an example
of what I'd like to do. This is not proper C syntax, but hopefullly
the meaning is clear.
/* File: a.c */
int data[6];
/* File: b.c */
extern int data[6];
data[0, 1, 2] = { 100, 200, 300 }; /* (file scope) */
/* File: c.c */
extern int data[6];
data[3, 4, 5] = { 400, 500, 600 }; /* (file scope) */
One obvious way to achieve this is to define an initialization
function in each of b.c and c.c, and call them at the start of the
program. However, I would prefer the array to get initialized
automatically.
Is there any way to do this? Any thoughts are much appreciated.