3

I am trying to share a global array between two files as below:

main.c:

#include <stdio.h>
#include "another.h"
int n=10;
int a[n];

void main(){
  printf("Enter value:\n");
  scanf("%d",&a[0]);
  display();
}

another.h:

#include <stdio.h>
extern int n;
extern int a[n];

void display(){
  printf("%d",a[0]);
  return;
}

However, I am getting the following error:

variable modified 'a' at file scope

I'm not able to understand why I'm getting this error. Does anyone have an explanation?

Olivia Stork
  • 4,660
  • 5
  • 27
  • 40
Biwadeep
  • 31
  • 4
  • Does this answer your question? [declaring a variable-length array as a global variable in C](https://stackoverflow.com/questions/10360394/declaring-a-variable-length-array-as-a-global-variable-in-c) – kaylum Nov 25 '20 at 21:29
  • Global variable length arrays are not valid in C. An alternative is to replace `n` with `#define MY_ARRAY_LEN 10` – kaylum Nov 25 '20 at 21:30
  • "There's no way to declare a variable length array as a global variable in C as it would have to be allocated before the knowing its size so the compiler can't know how much memory it should allocate for it" this is what the link you shared says. However, I am defining the size of the array n as 10 before initializing the array so that the compiler knows exactly how much memory is to be allocated. So even in spite of that it won't work? – Biwadeep Nov 25 '20 at 21:40

1 Answers1

0

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]};

   ...
ryyker
  • 22,849
  • 3
  • 43
  • 87
  • 1
    Alright, I see. So how can I share an array between two files? Can you give an example maybe? – Biwadeep Nov 25 '20 at 21:49
  • As long as you do not need it to be a _variable length array, an array can be declared with `extern` scope, and then defined in one of the .c files, _before its first use_, just like the variable I illustrated above. – ryyker Nov 25 '20 at 21:54
  • 1
    Thanks for helping. – Biwadeep Nov 25 '20 at 21:55
  • @Biwadeep - edited in simple example of project global array `gArray`. See post. – ryyker Nov 25 '20 at 22:03