Define an array in C

I have several arrays of 450 element arrays (saving bitmap data for display on lcd screens.) I would like to place them under the heading and #define , but I keep getting compilation errors. How can I do this in C ?

#define numbers[450] {0, 1,etc...}

#define numbers {0, 1, etc...}

#define numbers[450] then set the numbers later

and much more...

+8
source share
3 answers

Well ... you certainly don't need to use a definition. Just add them to 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 need help with a compilation error, you should post your code.

+9
source

As you display on the LCD, I assume this is an embedded system.

Do not put data in the header.

Put the data in a regular C or C ++ file. Compile this. It can only contain data, this is normal and easy to update.

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

For example, in the images.c file:

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

Then images.h this:

 #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 the other .c files in the program can access them.

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

A variable declaration , for example. extern byte numbers2[MAX_NUMBERS2]; tells the C compiler that the last related program has an array variable called numbers2 . If the linker does not get this definition (from somewhere else), it will throw an error because there is no room for the selected variable.

Definition of a variable (not from outside), for example. byte numbers2[MAX_NUMBERS2]; effectively tells the C compiler that there is an array variable called numbers2 and it must allocate a place here in the resulting object code from this source file, and this will be used to store the value of the variable in the final linked program,

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

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

There is a more subtle problem. If during the first development of the program the header file includes only one source file, then the program will be correctly compiled and compiled. Then, later, if the second source file contains a header (maybe someone just split the source file into two files), the linker will throw a "multiple definitions" error. This can be very confusing because the program was used to compile and link, and obviously nothing has changed.

Summary
Never allocate space for a variable by placing the definition in a header file. Place variable declarations in header files only.

+8
source

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

 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 (there may be more elegant ways to do this):

 #define element(n,d) ==(n) ? d : #define my_const_array(i) (i) element(0,1) (i) element(1,2) (i) element(2,5) 0 
+1
source

Source: https://habr.com/ru/post/911540/


All Articles