I am getting error during initialization of nested structure containing two members
unsigned int
typedef void (*vfptr)(void);
.
The first structure or parent structure contains simply two variables as shown:
typedef struct Stype1 {
unsigned int uiVar;
vfptr vfptr1;
}Stype1;
The second structure contains above structure as array of struct:
typedef struct Stype2 {
Stype1 Stype1inst[4];
}Stype2;
Whenever I try to compile using gcc 12.2.1
I get error:
error: expected expression before ‘{’ token
30 | Stype2inst.Stype1inst[4] = {{1,(vfptr)Vfun1},
I also tried initializing the structures using designated initializes as mentioned here:
- https://stackoverflow.com/a/330867/2805824
- How to initialize a struct in accordance with C programming language standards
typedef struct Stype2 {
Stype1 Stype1inst[] = {
{.uiVar = 1 , .vfptr1 = Vfun1 }
};
}Stype2;
But I get error:
error: expected ‘:’, ‘,’, ‘;’, ‘}’ or ‘__attribute__’ before ‘=’ token
24 | Stype1 Stype1inst[] = {
|
Also tried compiling with -std=gnu99
and -std=c99
without any luck.
The entire code is pasted below:
#include <stdio.h>
typedef void (*vfptr)(void);
void Vfun1(){};
void Vfun2(){};
void Vfun3(){};
void Vfun4(){};
typedef struct{
unsigned int uiVar;
vfptr vfptr1;
}Stype1;
typedef struct{
Stype1 Stype1inst[4];
}Stype2;
Stype2 Stype2inst;
int main()
{
Stype2inst.Stype1inst[4] = {{1,(vfptr)Vfun1},
{2,(vfptr)Vfun2},
{3,(vfptr)Vfun3},
{4,(vfptr)Vfun4}};
return 0;
}