How to initialize a array with precompiler In C Programming Language
I want to initialize a array. But ı have two different initial values in compile time. So I want to do it in precompile time. My code is
static const U8 userFont[8][8] =
{
#if (LCD_LANGUAGE == LANG_1)
{ 0x0E, 0x09, 0x09, 0x1D, 0x09, 0x09, 0x0E, 0x00 },
{ 0x03, 0x0E, 0x11, 0x10, 0x10, 0x11, 0x0E, 0x00 },
{ 0x09, 0x06, 0x0F, 0x01, 0x02, 0x04, 0x0F, 0x00 },
{ 0x0E, 0x11, 0x10, 0x10, 0x15, 0x0E, 0x04, 0x00 },
{ 0x11, 0x0E, 0x11, 0x10, 0x10, 0x11, 0x0E, 0x00 },
{ 0x19, 0x06, 0x09, 0x04, 0x02, 0x09, 0x06, 0x00 },
{ 0x00, 0x01, 0x01, 0x01, 0x01, 0x03, 0x03, 0x03 },
{ 0x03, 0x03, 0x03, 0x01, 0x01, 0x01, 0x01, 0x01 }
#elif (LCD_LANGUAGE == LANG_2)
{ 0x0A, 0x00, 0x11, 0x11, 0x11, 0x11, 0x0E, 0x00 },
{ 0x04, 0x00, 0x1E, 0x04, 0x04, 0x04, 0x1E, 0x00 },
{ 0x0A, 0x00, 0x0E, 0x11, 0x11, 0x11, 0x0E, 0x00 },
{ 0x0E, 0x11, 0x10, 0x10, 0x11, 0x0E, 0x04, 0x00 },
{ 0x0F, 0x10, 0x10, 0x0E, 0x01, 0x1E, 0x04, 0x00 },
{ 0x0E, 0x00, 0x0F, 0x10, 0x17, 0x11, 0x0F, 0x00 },
{ 0x01, 0x01, 0x01, 0x01, 0x01, 0x03, 0x03, 0x03 },
{ 0x03, 0x03, 0x03, 0x01, 0x01, 0x01, 0x01, 0x01 }
#endif
};
But I get this error : "expected an expression". Error given at the end of array. 开发者_StackOverflowSo how can I solve this problem?
A couple of questions:
- Have you defined the type
U8
with something liketypedef unsigned char U8;
? - Have you defined
LCD_LANGUAGE
as eitherLANG_1
orLANG_2
?
The reason I ask is because this little snippet below compiles and runs just fine:
#include <stdio.h>
typedef unsigned char U8;
#define LCD_LANGUAGE LANG_1
static const U8 userFont[8][8] =
{
#if (LCD_LANGUAGE == LANG_1)
{ 0x0E, 0x09, 0x09, 0x1D, 0x09, 0x09, 0x0E, 0x00 },
{ 0x03, 0x0E, 0x11, 0x10, 0x10, 0x11, 0x0E, 0x00 },
{ 0x09, 0x06, 0x0F, 0x01, 0x02, 0x04, 0x0F, 0x00 },
{ 0x0E, 0x11, 0x10, 0x10, 0x15, 0x0E, 0x04, 0x00 },
{ 0x11, 0x0E, 0x11, 0x10, 0x10, 0x11, 0x0E, 0x00 },
{ 0x19, 0x06, 0x09, 0x04, 0x02, 0x09, 0x06, 0x00 },
{ 0x00, 0x01, 0x01, 0x01, 0x01, 0x03, 0x03, 0x03 },
{ 0x03, 0x03, 0x03, 0x01, 0x01, 0x01, 0x01, 0x01 }
#elif (LCD_LANGUAGE == LANG_2)
{ 0x0A, 0x00, 0x11, 0x11, 0x11, 0x11, 0x0E, 0x00 },
{ 0x04, 0x00, 0x1E, 0x04, 0x04, 0x04, 0x1E, 0x00 },
{ 0x0A, 0x00, 0x0E, 0x11, 0x11, 0x11, 0x0E, 0x00 },
{ 0x0E, 0x11, 0x10, 0x10, 0x11, 0x0E, 0x04, 0x00 },
{ 0x0F, 0x10, 0x10, 0x0E, 0x01, 0x1E, 0x04, 0x00 },
{ 0x0E, 0x00, 0x0F, 0x10, 0x17, 0x11, 0x0F, 0x00 },
{ 0x01, 0x01, 0x01, 0x01, 0x01, 0x03, 0x03, 0x03 },
{ 0x03, 0x03, 0x03, 0x01, 0x01, 0x01, 0x01, 0x01 }
#endif
};
int main (void) {
printf ("0x%02x 0x%02x\n", userFont[0][0], userFont[2][2]);
return 0;
}
producing:
0x0e 0x0f
LANG_1 AND LANG_2 is enum so in procompile time this objects are invalid. So I replace wtih integral values my problem is solved. Thanks.
精彩评论