The following appears to be an attempt at creating a variable length array. But regardless what it is actually, the 2nd of the following two lines is not legal:
extern int n;
extern int a[n];
n
is not yet defined at the time it is used to create a
.
In case you were thinking to create a VLA...
by definition VLAs are only created with automatic storage duration on the stack. This makes them unusable for use as extern
or for any type of globally scoped variable.
However you can declare the size
variable as an extern
in a header file so that it is project global for any source file that includes that file, and once it is defined it can be used to size any array within project scope:
<some.h>
extern size_t array_size;
extern int gArray[100];
<some.c>
#include "some.h"
...
size_t array_size = 20;
int gArray[100] = {0};//Not a VLA
...
int main(void)
{
int array[array_size] = {0};
for(int i=0;i<sizeof(gArray);i++
{
//populate each element of gArray here
}
...
<someother.c>
#include "some.h"
int otherFunc(void)
{
//gArray is visible here just as it is in main(), with exactly the same values.
int local array[array_size] = {gArray[0],gArray[1],gArray[2],...,gArray[19]};
...