External Delaration for An Array?

前端 未结 6 2199
挽巷
挽巷 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:14

    If you want your array size to be accessible as a compile-time constant, then you have no other choice but to specify array size explicitly in the extern declaration of the array

    extern int a[9];
    

    In this case it becomes your responsibility to make sure that array size is consistent between the extern declaration and definition. You can use a manifest constant for that, but still it is going to be your responsibility to make sure that the number of initializers between the {} and the declared size are the same.

    If you don't care to have the array size as a compile-time constant, then you can do what Mark Wilkins suggests in his answer.

提交回复
热议问题