Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C:常量结构数组中的外部常量int_C_Arrays_Struct_Constants_Extern - Fatal编程技术网

C:常量结构数组中的外部常量int

C:常量结构数组中的外部常量int,c,arrays,struct,constants,extern,C,Arrays,Struct,Constants,Extern,当使用外部常量整数初始化结构数组时,我收到一条错误消息“表达式必须具有常量值” 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

当使用外部常量整数初始化结构数组时,我收到一条错误消息“表达式必须具有常量值”

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);
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);
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.
};
File2.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);
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);
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.
};
获取字节。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);
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);
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.
};
我还从
Array\u Attributes
size
字段中删除了
const
限定符,并得到了相同的错误消息

data1\u size
data2\u size
const unsigned int
但在不同的转换单元中时,为什么编译器会抱怨常量值表达式

我想要一个在编译时生成的[array address,array size]常量数组


我使用的是Green Health CCAM< <代码> 4.24,在WindowsXP上,C语言<强> > > > > C++ > .< P> C >代码> const 限定符与编译器认为的代码<常数表达式< /代码>无关。在初始值设定项中,即

const struct attributes attrs[] = {
    { expr1, expr2 }, 
    ...
}
expr1
expr2
必须具有编译器可以接受的非常特定的形式。这些限制的结果是,表达式可以在不从程序变量获取的情况下进行求值,因为这些变量在编译时不存在

您正在尝试使用
data1\u size
data2\u size
,根据这些规则,它们不是编译时常量

顺便说一下,声明

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

不兼容,将导致代码中出现错误。后者应该是

extern const unsigned char data1[];

我的理解是
data1[]
*data1
是相同的。它们有什么不同,为什么会导致错误?它们唯一可以被替换的地方是在参数声明中
extern char*foo
告诉编译器
foo
是一个包含字符指针的4字节变量
extern char bar[]
告诉编译器
bar
是一个地址常量,指向未知大小的字符块
foo[i]
首先获取4字节指针,添加
i
,然后取消引用以获取字符
bar[i]
获取值
bar
,添加
i
,并取消引用。