External Delaration for An Array?

前端 未结 6 2223
挽巷
挽巷 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 08:50

    Put the variable in the header file only like this:

    #ifndef A_DEF
    #define A_DEF
    int a[] = {1,2,3,4,5,6,7,8,9}; 
    #endif
    

    The conditional compilation will not allow variable redefinition. Thus you have access to it from anywhere.

提交回复
热议问题