int arr[10]; //in file1.c extern int *arr; //in file2.c main() { arr[0]=1; } //

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • abhi17
    New Member
    • Feb 2010
    • 6

    int arr[10]; //in file1.c extern int *arr; //in file2.c main() { arr[0]=1; } //

    int arr[10]; //in file1.c
    extern int *arr; //in file2.c
    main()
    {
    arr[0]=1;
    }
    this code gave error saying undefined reference to arr, why???
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    your declaration

    extern int *arr;

    does not match the definition

    int arr[10];

    Which is why you should put the declaration into a header file and then include that header into both C files.

    In all likely hood the reason you get an undefined reference to arr is either that you only linked with the object from compiling file2.c or that the difference between declaration and definition is somehow causing the linker to ignore the defined array.

    Comment

    Working...