default value for struct member in C

后端 未结 16 2145
-上瘾入骨i
-上瘾入骨i 2020-11-27 10:53

Is it possible to set default values for some struct member? I tried the following but, it\'d cause syntax error:

typedef struct
{
  int flag = 3;
} MyStruct         


        
相关标签:
16条回答
  • 2020-11-27 11:26

    If you only use this structure for once, i.e. create a global/static variable, you can remove typedef, and initialized this variable instantly:

    struct {
        int id;
        char *name;
    } employee = {
        .id = 0,
        .name = "none"
    };
    

    Then, you can use employee in your code after that.

    0 讨论(0)
  • 2020-11-27 11:27

    You can create a function for it:

    typedef struct {
        int id;
        char name;
    } employee;
    
    void set_iv(employee *em);
    
    int main(){
        employee em0; set_iv(&em0);
    }
    
    void set_iv(employee *em){
        (*em).id = 0;
        (*em).name = "none";
    }
    
    0 讨论(0)
  • 2020-11-27 11:31

    you can not do it in this way

    Use the following instead

    typedef struct
    {
       int id;
       char* name;
    }employee;
    
    employee emp = {
    .id = 0, 
    .name = "none"
    };
    

    You can use macro to define and initialize your instances. this will make easiier to you each time you want to define new instance and initialize it.

    typedef struct
    {
       int id;
       char* name;
    }employee;
    
    #define INIT_EMPLOYEE(X) employee X = {.id = 0, .name ="none"}
    

    and in your code when you need to define new instance with employee type, you just call this macro like:

    INIT_EMPLOYEE(emp);
    
    0 讨论(0)
  • 2020-11-27 11:32

    Create a default struct as the other answers have mentioned:

    struct MyStruct
    {
        int flag;
    }
    
    MyStruct_default = {3};
    

    However, the above code will not work in a header file - you will get error: multiple definition of 'MyStruct_default'. To solve this problem, use extern instead in the header file:

    struct MyStruct
    {
        int flag;
    };
    
    extern const struct MyStruct MyStruct_default;
    

    And in the c file:

    const struct MyStruct MyStruct_default = {3};
    

    Hope this helps anyone having trouble with the header file.

    0 讨论(0)
  • 2020-11-27 11:34

    You can implement an initialisation function:

    employee init_employee() {
      empolyee const e = {0,"none"};
      return e;
    }
    
    0 讨论(0)
  • 2020-11-27 11:34

    Another approach to default values. Make an initialization function with the same type as the struct. This approach is very useful when splitting large code into separate files.

    struct structType{
      int flag;
    };
    
    struct structType InitializeMyStruct(){
        struct structType structInitialized;
        structInitialized.flag = 3;
        return(structInitialized); 
    };
    
    
    int main(){
        struct structType MyStruct = InitializeMyStruct();
    };
    
    0 讨论(0)
提交回复
热议问题