-1

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 ?

monk
  • 1,953
  • 3
  • 21
  • 41
  • None of the files defines `cpu_list`; they only declare it. You need `int cpu_list[SOMESIZE];` in one of the source files (or perhaps in a fourth file) — but not in the header. Or you need `int cpu_list[] = { 1, 2, 3, 9, 27 };` or whatever to initialize it and determine its size. See also [How do I use `extern` to share variables between source files](https://stackoverflow.com/questions/1433204/how-do-i-use-extern-to-share-variables-between-source-files), but try not to be scared by the main answer. – Jonathan Leffler Apr 15 '19 at 23:43
  • Hello Jonathan, in fileA.c I did `int cpu_list[2];` – monk Apr 15 '19 at 23:46
  • 1
    Note that the `int cpu_list[2]` in `not_main` is a _local_ variable, not a _global_ one, and isn't the variable referenced in your `extern init cpu_lst[]` declaration. It isn't visible to any other function. – 1201ProgramAlarm Apr 15 '19 at 23:47
  • Oh, yes, sort of. You hid the `extern` variable with a local variable. Inside `not_main()`, `cpu_list` refers to the local variable and not the global variable. You have to define global variables outside any function — I didn't even look inside the function because it was not going to be relevant. If you compile with GCC, add the `-Wshadow` option to get warnings about when a local variable shadows (conceals) a global variable. It also appears you have an infinitely recursive function in `not_main()` which eventually crash with a [stack overflow](https://stackoverflow.com/). – Jonathan Leffler Apr 15 '19 at 23:47
  • even after moving `int cpu_list[2]` out of all the functions, the values getting printed are only 0` although the values from `not_main` are correct but in infinite loop. – monk Apr 15 '19 at 23:55
  • When I run a minor adaptation of your code (I declared `not_main()` in the header, moved `int cpu_list[2];` outside `not_main()`, and gave the functions formal prototype definitions using `(void)` instead of obsolescent `()` and added `#include ` and `sleep(1);` before the recursive call), then I got `0, 0` followed by `5, 7` repeatedly as the output. That's what I'd expect since you set the values in `cpu_list` ab initio on each recursive invocation. – Jonathan Leffler Apr 16 '19 at 00:09
  • I have updated the question as per the advice, I still do not understand why I am not getting `5,7` in the main function , I am getting zero there! – monk Apr 16 '19 at 00:11
  • Did you remember to delete the definition of `cpu_list` from `not_main()`? – Jonathan Leffler Apr 16 '19 at 00:11
  • yes, I did in the core, I missed to do in on SO. Updated – monk Apr 16 '19 at 00:13
  • 1
    The zeros are because `int cpu_list[2];` outside a function is default initialized to both elements `0`, of course. So when `main()` prints the elements, they're supposed to be `0`. – Jonathan Leffler Apr 16 '19 at 00:14
  • It leads me back to the main issue, how to get 5,7 in main ? :) – monk Apr 16 '19 at 00:15
  • HI Jonathan , got it . Thanks for your kind advice. – monk Apr 16 '19 at 00:17
  • If you print out the values of cpu_list `after` the call to `not_main`, you will see that they have been updated correctly. – bruceg Apr 16 '19 at 00:28

1 Answers1

1

Given the source below, I get the output:

cpu_list[0] is 0
cpu_list[1] is 0
cpu_list[0] is 5 in not_main function 
cpu_list[1] is 7 in not_main function 
cpu_list[0] is 5
cpu_list[1] is 7

The key changes are moving the definition of cpu_list to file scope in fileA.c, and not recursing in not_main(), and printing the values again in main() after not_main() returns. A minor change is losing j; it was not providing any value as it was just a copy of i.

Note that it is important (I'm tempted to say 'crucial') that both fileA.c and fileB.c include the header. That gives you the cross-checking that's necessary to ensure that both the array and the function are defined and declared consistently.

my_files.h

extern int cpu_list[];
extern int not_main(void);

fileA.c

#include <stdio.h>
#include "my_files.h"

int cpu_list[2];

int not_main(void)
{
    int i;
    for (i = 0; i < 2; i++)
    {
        cpu_list[i] = 2 * i + 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 main(void)
{
    printf("cpu_list[0] is %d\n", cpu_list[0]);
    printf("cpu_list[1] is %d\n", cpu_list[1]);

    not_main();

    printf("cpu_list[0] is %d\n", cpu_list[0]);
    printf("cpu_list[1] is %d\n", cpu_list[1]);

    return 0;
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278