我有以下情况:
typedef struct A {
unsigned int a[4];
} A_;
int main() {
unsigned int b[4] = {1,2,3,4};
A_ a = {b};
}
这让我收到以下警告:
warning: initialization of 'unsigned int' from 'unsigned int *' makes integer from pointer without a cast
但是使这个 A_ a = {{1,2,3,4}};
没问题。为什么?
最佳答案
标准 C 不提供任何机制来用数组初始化结构成员,除了用字符串文字初始化数组。
相反,您可以使用结构初始化结构。假设在编写代码时已知要使用的值,则后一种结构也可以制作成static const
:
int main(void)
{
static const A_ InitialStructure = {{ 1, 2, 3, 4 }};
A_ a = InitialStructure;
}
关于c - 使用先前声明的数组初始化结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57082904/