是否可以在不使用函数的情况下初始化const结构?

qui*_*nge 12 c

我在一些C代码中有一个相当简单的const结构,它只包含几个指针,并希望在可能的情况下静态初始化它.我可以,如果是这样,怎么样?

Lev*_*Lev 15

如果指针指向全局对象,则可以:

// In global scope
int x, y;
const struct {int *px, *py; } s = {&x, &y};
Run Code Online (Sandbox Code Playgroud)


ASh*_*lly 5

const struct mytype  foo = {&var1, &var2};
Run Code Online (Sandbox Code Playgroud)