在C中初始化struct

In C we cannot assign int variable = true;, where as the below code executes.

typedef struct mystruct {
    int variable;
} mystruct_;

int main(void){
    // Your code here!
    mystruct_ st = {true};
    printf("%i",st.variable);
}

有人可以解释为什么吗?

评论
  • eipsum
    eipsum 回复

    In C we cannot assign int variable = true;

    这是不正确的。它可以编译并成功运行。以下代码可在GCC上编译并完美运行。

    #include <stdio.h>
    #include <stdbool.h>
    
    typedef struct mystruct {
        int variable;
    } mystruct_;
    
    int main(void){
        int variable = true;
        mystruct_ st = {true};
        printf("%i %i",st.variable, variable);
    }