External Delaration for An Array?

前端 未结 6 2200
挽巷
挽巷 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.

    0 讨论(0)
  • 2020-12-17 08:54

    I would #define ARRAY_MAX (or whatever) in an external header generally used for defining such things, then include that header in both files that need it. This works well when you tend to keep most if not all of your defines in one or two central headers.

    0 讨论(0)
  • 2020-12-17 08:58

    in my opinion if you don't have definition and define with sizeof a in one file it doesn't compile.

    Files are compile and stored as *.obj / *.a files. You can use arrays from another files thanks to extern declaration which will be check in linking process, after compilation.

    You want to declare define (preprocesor must help here with this. It is run before compilator).

    so before compilation you won't get array from another file...

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-17 09:14

    You might put something like the following into a.c and then extern it from b.c.

    In a.c:

    int a[] = {1, 2, 3};
    const int lengthofa = sizeof( a ) / sizeof( a[0] );
    

    And then in b.c:

    extern int a[];
    // the extern (thanks Tim Post) declaration means the actual storage is in another 
    // module and fixed up at link time.  The const (thanks Jens Gustedt) prevents it
    // from being modified at runtime (and thus rendering it incorrect).
    extern const int lengthofa;
    
    void somefunc() {
      int i;
      for ( i = 0; i < lengthofa; i++ )
        printf( "%d\n", a[i] );
    }
    
    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题