结构中的成员可以初始化为值吗?

lem*_*des 4 c struct initialization

我只是想知道在结构中声明和定义的变量是否可以初始化为某个值,计划使用函数指针来模拟OOP中的类.

示例COde:

typedef struct{
int x;
int (*manipulateX)(int) = &manipulateX;
}x = {0};

void main()
{
    getch();
}

int manipulateX(int x)
{
    x = x + 1;
return x;
}
Run Code Online (Sandbox Code Playgroud)

das*_*ght 9

从C99开始,您可以使用指定的初始值设定项将结构字段设置为值,如下所示:

struct MyStruct {
    int x;
    float f;
};

void test() {
    struct MyStruct s = {.x=123, .f=456.789};
}
Run Code Online (Sandbox Code Playgroud)