【发布时间】:2016-08-14 10:57:27
【问题描述】:
所以我有一个struct:
typedef struct myStruct
{
const int *const array_ptr;
} myStruct_s;
我有一个const 数组int:
const int constArray[SIZE1] =
{
[0] = 0,
[1] = 1,
[2] = 2,
//...
};
现在我有一个myStruct_s 的const 数组,使用指定的初始化程序进行了初始化:
const myStruct_s structArray[SIZE2] =
{
[0] =
{
.array_ptr = &constArray
},
//...
}
我收到警告:
“const int (*)[SIZE1]”类型的值不能用于初始化 “const int *const”类型的实体
如何正确初始化这个指针?
我想避免:
const myStruct_s structArray[SIZE2] =
{
[0] =
{
.array_ptr = (const int *const) &constArray
},
//...
}
如果可能的话,因为我觉得我告诉编译器“我不知道我在做什么,请不要检查类型”...
感谢您的帮助:)。
【问题讨论】:
-
array_ptr = &constArray[0]; -
数组索引从 0 开始。您的作业 (
[1] = 1) 从 1 开始。与问题无关,但看起来很可疑。 -
@davmac:你是对的!实际上我使用
enum作为索引来填充数组。我从不使用幻数:)。我的代码简化得太快了!
标签: c arrays pointers initialization constants