Global array variables

  • Array variables defined inside some function are local array variables

  • Array variables defined outside a function are global array variables

  • Example:

     int A[10];        // Global array variable
    
     int main( )
     {
        double B[10];  // Local array variable
     }
      

Declaring global array variables

  • Recall the variable declaration requirement:

      • The compiler requires data type information of variables to perform the correct translation

  • The data type information requirement also apply to array variables:

      • If an (global) array variable is used before it is defined, we must declare the data type before the use

  • A example will be presented next

Example of using an global 1-dimensional array variable

In this example, we define the global array variable before it is used:

#include <stdio.h>

int   A[10];    // Define (global) array before use

int main(int argc, char *argv[] )
{
   int i, j;

   for (i = 0; i < 10; i++)
      A[i] = i;                     // Use array

   for (i = 0; i < 10; i++)
      printf("%d ", A[i]);          // Use array
   printf("\n\n");
}


  

There is no compiler error
DEMO: demo/C/set1/declare-array3.c

Example of using an global 1-dimensional array variable

If we define the global array variable after it is used:

#include <stdio.h>



int main(int argc, char *argv[] )
{
   int i, j;

   for (i = 0; i < 10; i++)
      A[i] = i;                     // Use array

   for (i = 0; i < 10; i++)
      printf("%d ", A[i]);          // Use array
   printf("\n\n");
}

int   A[10];    // Define (global) array after use
  

There will be a undeclared identifier compiler error
DEMO: demo/C/set1/declare-array3.c

Example of using an global 1-dimensional array variable

We can fix the error by declaring the global array variable before it is used:

#include <stdio.h>

extern int   A[10];    // Declare (global) array before use

int main(int argc, char *argv[] )
{
   int i, j;

   for (i = 0; i < 10; i++)
      A[i] = i;                     // Use array

   for (i = 0; i < 10; i++)
      printf("%d ", A[i]);          // Use array
   printf("\n\n");
}

int   A[10];    // Define (global) array after use
  

The declaration "extern int A[10];" tells the C compiler that the name A is an array ([ ]) of integers (int)

DEMO: demo/C/set1/declare-array3.c

The official syntax to declare global 1-dimensional array variable

The array size is optional in the array declaration of a 1-dimensional array:

#include <stdio.h>

extern int   A[  ];    // Declare (global) array before use

int main(int argc, char *argv[] )
{
   int i, j;

   for (i = 0; i < 10; i++)
      A[i] = i;                     // Use array

   for (i = 0; i < 10; i++)
      printf("%d ", A[i]);          // Use array
   printf("\n\n");
}

int   A[10];    // Define (global) array after use
  

( because the array length information are not retained by the C compiler)

DEMO: demo/C/set1/declare-array3.c

Example of using an global 2-dimensional array variable

In this example, we define the global 2-dimensional array variable before it is used:

#include <stdio.h>

int   B[4][3];    // Define (global) array before use

int main(int argc, char *argv[] )
{
   int i, j;

   for (i = 0; i < 4; i++)
      for (j = 0; j < 3; j++)
         B[i][j] = i+j;           // Use array

   for (i = 0; i < 4; i++)
   {
      for (j = 0; j < 3; j++)
         printf("%d ", B[i][j]);  // Use array
      printf("\n");
   }
   printf("\n\n");
}


There is no compiler error
DEMO: demo/C/set1/declare-array4.c

Example of using an global 2-dimensional array variable

If we define the global 2-dimensional array variable after it is used:

#include <stdio.h>



int main(int argc, char *argv[] )
{
   int i, j;

   for (i = 0; i < 4; i++)
      for (j = 0; j < 3; j++)
         B[i][j] = i+j;           // Use array

   for (i = 0; i < 4; i++)
   {
      for (j = 0; j < 3; j++)
         printf("%d ", B[i][j]);  // Use array
      printf("\n");
   }
   printf("\n\n");
}

int   B[4][3];    // Define (global) array after use  

There will be a undeclared identifier compiler error
DEMO: demo/C/set1/declare-array3.c

Example of using an global 2-dimensional array variable

We can fix the error by declaring the global 2-dimensional array variable before it is used:

#include <stdio.h>

extern int   B[4][3];    // Declare (global) array before use

int main(int argc, char *argv[] )
{
   int i, j;

   for (i = 0; i < 4; i++)
      for (j = 0; j < 3; j++)
         B[i][j] = i+j;           // Use array

   for (i = 0; i < 4; i++)
   {
      for (j = 0; j < 3; j++)
         printf("%d ", B[i][j]);  // Use array
      printf("\n");
   }
   printf("\n\n");
}

int   B[4][3];    // Define (global) array after use  

DEMO: demo/C/set1/declare-array4.c

The official syntax to declare global 2-dimensional array variable

The first dimension size is optional in the array declaration of a 2-dimensional array:

#include <stdio.h>

extern int   B[ ][3];    // Declare (global) array before use

int main(int argc, char *argv[] )
{
   int i, j;

   for (i = 0; i < 4; i++)
      for (j = 0; j < 3; j++)
         B[i][j] = i+j;           // Use array

   for (i = 0; i < 4; i++)
   {
      for (j = 0; j < 3; j++)
         printf("%d ", B[i][j]);  // Use array
      printf("\n");
   }
   printf("\n\n");
}

int   B[4][3];    // Define (global) array after use   

(Reason explained next....)
DEMO: demo/C/set1/declare-array4.c

Why is the first dimension not necessary in an array declaration

  • Recall:   elements in 2 dimensional arrays are stored row-wise:

  • Recall:   the C compiler do not record the length of arrays

    I.e.:

    • The C compiler do not record the # rows of a 2-dim array

Why is the 2nd dimension necessary in an array declaration

  • The array element B[i][j] is accessed using a one-dimensional index as B[i*3 + j]:

  • The 2nd dimension is necessary to linearize the index used to access array elements:

    • The C compiler needs to know the # columns to access the array element B[i][j]