Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Define Array in C

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 I keep getting compilation errors. How would 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 many more...

like image 678
Reid Avatar asked Mar 23 '12 21:03

Reid


People also ask

What is array in C definition?

Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value. To create an array, define the data type (like int ) and specify the name of the array followed by square brackets [].

What is an array in C with example?

An array is a variable that can store multiple values. For example, if you want to store 100 integers, you can create an array for it. int data[100];

What is array in C and types?

Arrays are the derived data type in C programming language which can store the primitive type of data such as int, char, double, float, etc. It also has the capability to store the collection of derived data types, such as pointers, structure, etc.

How do you #define an array?

An array is a data structure that contains a group of elements. Typically these elements are all of the same data type, such as an integer or string. Arrays are commonly used in computer programs to organize data so that a related set of values can be easily sorted or searched.


2 Answers

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.

like image 84
Ed S. Avatar answered Sep 30 '22 14:09

Ed S.


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.

like image 43
gbulmer Avatar answered Sep 30 '22 15:09

gbulmer