[英]Initializing struct with union of arrays
我注意到有很多关于这个主题的帖子,但我似乎无法指出任何有帮助的内容。
我定义了以下代码:
typedef struct
{
float re;
float im;
} MyComplex;
typedef struct
{
float rf;
union
{
float noise[4];
MyComplex iq[4];
};
} RfTable_t;
RfTable_t Noise[2] =
{
{ 1.2f, .noise=0.f },
{ 2.1f, .noise=0.f };
};
**EDIT - Add function Test**
void Test()
{
Noise[0].rf = 2.1f;
Noise[0].noise[0] = 3.2f;
}
我正在尝试静态定义全局变量Noise
。 我收到以下错误:
expected primary expression before '{' token
expected primary expression before '{' token
expected primary expression before '}' before '{' token
expected primary expression before '}' before '{' token
expected primary expression before ',' or ';' before '{' token
expected declaration before '}' token
要初始化的任何结构、联合和/或数组都需要自己的一组花括号来初始化它。 具体来说,联合需要一组大括号,联合内部的float
组也需要大括号:
RfTable_t Noise[2] =
{
{ 1.2f, { .noise={0.f} } },
{ 2.1f, { .noise={0.f} } }
};
还要注意你有一个流浪;
初始化器内部。
我做了最小的更改以使其编译:
#include <stdio.h>
typedef struct
{
float re;
float im;
} MyComplex;
typedef struct
{
float rf;
union
{
float noise[4];
MyComplex iq[4];
};
} RfTable_t;
RfTable_t Noise[2] =
{
{ 1.2f, .noise={0.f} }, // Initialize NOISE with {0.f} instead of 0.f.
{ 2.1f, .noise={0.f} } // Remove extra semi-colon.
};
int main(void) {
return 0;
}
简单来说:
noise
是一个数组。
要初始化它,您必须使用数组初始值设定项语法:
{ value, value, value }
您的0.f
值周围没有括号。
此外,您还有一个额外的分号。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.