I have following three files and I need to call an dynamically populated array from one file into other file, I am getting compilation error, can someone please help me. Banging head from long time.
my_files.h:
extern int cpu_list[];
fileA.c:
#include<stdio.h>
#include "my_files.h"
int not_main()
{
int i,j;
for(i=0;i<2;i++){
j=i;
cpu_list[i]=2*j + 5;
}
printf("cpu_list[0] is %d in not_main function \n",cpu_list[0]);
printf("cpu_list[1] is %d in not_main function \n",cpu_list[1]);
return 0;
}
fileB.c:
#include <stdio.h>
#include "my_files.h"
int cpu_list[2];
int main()
{
printf("cpu_list[0] is %d in main function \n",cpu_list[0]);
printf("cpu_list[1] is %d in main function\n",cpu_list[1]);
not_main();
return 0;
}
compilation:
gcc -c fileA.c
gcc -c fileB.c
gcc -o out fileA.o fileB.o
cpu_list[0] is 0 in main function
cpu_list[1] is 0 in main function
cpu_list[0] is 5 in not_main function
cpu_list[1] is 7 in not_main function
WHY the value of main function are showing zero for the array ?