C: External const ints in array const struct

I get the error "expression must have a constant value" when initializing an array of structures with an external constant integer.

file1.c:

const unsigned char data1[] =
{
    0x65, 0xF0, 0xA8, 0x5F, 0x5F,
    0x5F, 0x5F, 0x31, 0x32, 0x2E,
    0x31, 0xF1, 0x63, 0x4D, 0x43, 
    0x52, 0x45, 0x41, 0x54, 0x45,
    0x44, 0x20, 0x42, 0x59, 0x3A,
    0x20, 0x69, 0x73, 0x70, 0x56, 
// ...
};
const unsigned int data1_size = sizeof(data1);

file2.c:

const unsigned char data2[] =
{
    0x20, 0x44, 0x61, 0x74, 0x61,
    0x20, 0x52, 0x6F, 0x77, 0x20,
    0x3D, 0x20, 0x34, 0x38, 0x12, 
//...
};
const unsigned int data2_size = sizeof(data2);

Get_Byte.c:

extern const unsigned char * data1;
extern const unsigned int    data1_size;
extern const unsigned char * data2;
extern const unsigned int    data2_size;

struct Array_Attributes
{
    const unsigned char *    p_data;
    const unsigned int       size;
};

const struct Array_Attributes Data_Arrays[] =
{
    {data1, data1_size},  // Error message is for data1_size here.
    {data2, data2_size},  // Another error message generated for data2_size here.
};

I also removed the classifier constfrom the field size Array_Attributesand received the same error message.

Why does the compiler complain about expressing a constant value when data1_sizeand data2_sizeare const unsigned int, but in a different unit of translation?

I need a constant array from [array address, array size] that is generated at compile time.

I am using Green Hills ccarm4.24, on Windows XP, the C language is NOT C ++.

+3
1

C const , constant expression, . , ..

const struct attributes attrs[] = {
    { expr1, expr2 }, 
    ...
}

expr1 expr2 , . , , .

data1_size data2_size, .

,

const unsigned char data1[] = { ... };

extern const unsigned char *data1;

.

extern const unsigned char data1[];
+7

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


All Articles