External Delaration for An Array?

前端 未结 6 2214
挽巷
挽巷 2020-12-17 08:27

I have an array defined in a file and in another I have to use it, for e.g-

/* a.c - defines an array */

int a[] = {1,2,3,4,5,6,7,8,9}; 


/* b.c - declare          


        
6条回答
  •  既然无缘
    2020-12-17 09:08

    The compiler does not have enough information when compiling b.c to determine the size of the array. That information is only in a.c where your initializer list is in scope.

    You will have to communicate the size somehow. One method is to define an int const with the size and extern that as well. Another possibility would be to use a sentinel (a value outside your valid data range) to indicate the end of the array.

提交回复
热议问题