Define Array in C

前端 未结 3 1967
盖世英雄少女心
盖世英雄少女心 2021-01-02 03:53

I have several 450 element character arrays (storing bitmap data to display on lcd screens.) I would like to put them under a header file and #define them, but

相关标签:
3条回答
  • 2021-01-02 04:23

    Well... you certainly don't need to use a define. Just add them into the header as const, static arrays.

    /* prevents multiple, redundant includes */
    /* make sure to use a symbol that is fairly sure to be unique */
    #ifndef TEST_H
    #define TEST_H
    
    /* your image data */
    const char image[] = { 1, 2, 3, 4, ... };
    
    #endif
    

    Also, if you want help on a compilation error then you should post your code.

    0 讨论(0)
  • 2021-01-02 04:27

    I have had a similar problem. In my case, I needed an array of constants in order to use as size of other static arrays. When I tried to use the

    const int my_const_array[size] = {1, 2, 3, ... };
    

    and then declare:

    int my_static_array[my_const_array[0]];
    

    I get an error from my compiler:

    array bound is not an integer constant
    

    So, finally I did the following (Maybe there are more elegant ways to do that):

    #define element(n,d) ==(n) ? d :
    #define my_const_array(i) (i) element(0,1) (i) element(1,2) (i) element(2,5) 0
    
    0 讨论(0)
  • 2021-01-02 04:43

    Because you are displaying on an LCD, I am assuming this is an embedded system.

    Don't put the data into a header.

    Put the data into an ordinary C or C++ file. Compile this. It might only contain the data, that is okay, and makes it easy to update.

    Then use the header file to give access to the data.

    For example, in a images.c file:

    #include "images.h"
    const byte numbers1[MAX_NUMBERS1] = { ... };
    byte numbers2[MAX_NUMBERS2];       // will be initialsied to 0
    

    Then images.h is:

    #ifndef _IMAGES_H_
    #define _IMAGES_H_
    
    typedef unsigned char byte;
    #define MAX_NUMBERS1 (450)
            // different constants in case you change something        
    #define MAX_NUMBERS2 (450)      
           // even better if you can do const static int MAX_NUMBERS1=450; 
           // but depends on the compiler
    extern const byte numbers1[MAX_NUMBERS1] = { ... };
    extern byte numbers2[MAX_NUMBERS2];       // will be initialised to 0
    
    #endif
    

    Then all other .c files in the program can access them.

    It is (almost) always a bad idea to put a definition of a variable into a header file.

    A declaration of a variable, eg. extern byte numbers2[MAX_NUMBERS2]; is telling the C compiler that there is an array variable called numbers2 somewhere else in the final, linked program. If the linker doesn't get that definition (from somewhere else) then it will raise an error because there is no space for the variable allocated.

    A definition of a variable (notice no extern), eg. byte numbers2[MAX_NUMBERS2]; is effectively telling the C compiler that there is an array variable called numbers2 and it should allocate the space here, in the resulting object code from this source file, and this will be used to hold the value of the variable in the final, linked program.

    The space for numbers2 is not allocated by the C compiler when it sees a declaration (preceded by extern), it is allocated when it sees the actual definition (no extern).

    So, if you put the actual definition of any variable in a header file, and include it into more than one source code files (.c), the C compiler will allocate space for the variable more than once. Then the linker will give an error (usually multiple definitions of the same name).

    There is a more subtle problem. If, when first developing the program, the header file is only included is one source file, then the program will compile and link correctly. Then, at a later date, if a second source file includes the header (maybe someone has just split the original source code file into two files), the linker will raise a 'multiple definitions' error. This can be very confusing because the program used to compile and link, and apparently nothing has changed.

    Summary
    Never allocate space for a variable by putting a definition in a header file. Only put variable declarations in header files.

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