by Dinesh Thakur Category: Array

As in case of scalar variables, we can also use external or global arrays in a program, i. e., the arrays which are defined outside any function. These arrays have global scope.

 

Thus, they can be used anywhere in the program. They are created in the beginning of program execution and last till its end. Also, all the elements of these arrays are initialized to default values (zero for arithmetic types and NULL for pointers). Such arrays are usually defined at the beginning of a program, as illustrated below.

#include <stdio.h>

void ivec_read();

void ivec_print();

int a[10];     /* external array */

int n;

int main()

{

      ivec_read();

      ivec_print();

      return 0;

}

void ivec_read ()

{

      int i;

      scanf("%d", &n);

      for(i = 0; i < n; i++)

      scanf("%d", &a[i]);

}

void ivec_print()

{

     int i;

     for(i = 0; i < n; i++)

     printf("%d ", a[i]);

     printf("\n");

}

Note that this approach avoids passing arrays as parameters and thereby simplifies the program and also saves some time. However, the use of global arrays has some limitations.

1. These functions work only with particular array(s) and cannot be used for performing similar operations on other arrays.

2. As the arrays can be modified from any part of the program, debugging such programs is more difficult. However, this approach can be particularly useful with const arrays that need to be shared among several functions. For example, the month_days array required in date processing applications.

As with scalar variables, external arrays can be defined anywhere in a program, but outside any function of course. If we use such an array before it is defined, we should provide an extern declaration, as illustrated in the program given below.

#include <stdio.h>

void ivec_read();

void ivec_print();

int n;

int main()

{

      ivec_read();

      ivec_print();

      return 0;

}

void ivec_read ()

{

      int i;

      extern int a[];         /* external array declaration */

      scanf("%d", &n);

      for(i = 0; i < n; i++)

      scanf("%d", &a[i]);

}

int a[10];     /* external array definition */

void ivec_print()

{

     int i;

     for(i = 0; i < n; i++)

     printf("%d", a[i]);

     printf("\n");

}

The extern declaration of an array should match with its definition with respect to array size, data type, type modifier and storage class; otherwise, the compiler will report an error. However, note that the array size can be omitted in an extern declaration. This declaration does not create any array and it can be specified any number of times, even with in the same scope.





About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.



Related Articles