I noticed there have been many posts about this subject but I cannot seem to pin point anything that would help.
I have defined the following code:
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;
}
I am trying to define the global variable Noise
statically. I get the following error:
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
Any structure, union, and/or array to be initialized needs its own set of curly braces to initialize it. Specifically, the union needs a set of braces, and the float
array inside the union also needs braces:
RfTable_t Noise[2] =
{
{ 1.2f, { .noise={0.f} } },
{ 2.1f, { .noise={0.f} } }
};
Note also that you had a stray ;
inside of the initializer.
I made the smallest changes I could to make it compile:
#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;
}
In brief:
noise
is an array.
To initialize it you must use the array initializer syntax:
{ value, value, value }
You did not have brackets around your value of 0.f
.
Also, you had an extra semi-colon.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.