使用数组联合初始化结构
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
任何要初始化的结构、联合、and/or数组都需要自己的一组花括号来初始化。具体来说,union需要一组大括号,union里面的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
值没有括号。
此外,您多了一个分号。
我注意到有很多关于这个主题的帖子,但我似乎无法指出任何有用的信息。
我定义了以下代码:
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
任何要初始化的结构、联合、and/or数组都需要自己的一组花括号来初始化。具体来说,union需要一组大括号,union里面的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
值没有括号。
此外,您多了一个分号。