Reputation: 79
//Program to understand Nested structs in C
#include <stdio.h>
typedef union test {
float tet;
struct {
int bite;
} p;
} U_test;
//union test U_test;
struct U_test.p = {.bite=150};
int main()
{
printf("%d\n", U_test.p.bite);
U_test.tet = 0.0;
printf("%d", U_test.p.bite);
return 0;
}
Here is the error code I keep seeing below. Not sure I am doing something wrong here or am I typing something wrong?Please advise. Thanks in advance.
error: expected identifier or ‘(’ before ‘.’ token
struct U_test.p = {.bite=150};
Upvotes: 0
Views: 28
Reputation: 14046
struct U_test.p = {.bite=150};
That is not the correct syntax to create a variable of the defined union type. It should be:
U_test u_test = { .p.bite = 150 };
After making that change all the references in main
which are currently U_test
need to be changed to u_test
.
Here is the full program corrected:
#include <stdio.h>
typedef union test{
float tet;
struct{
int bite;
}p;
}U_test;
//union test U_test;
U_test u_test = { .p.bite=150 };
int main()
{
printf("%d\n", u_test.p.bite);
u_test.tet = 0.0;
printf("%d", u_test.p.bite);
return 0;
}
Upvotes: 1