Is it better style to initialize a structure by passing a reference or returning it?

前端 未结 4 1843
栀梦
栀梦 2021-01-20 02:54

Say I have the following:

typedef struct {
    int x;
    int y;
    char a;
    char b;
} myStruct;

Is it better practice to create a new

4条回答
  •  遥遥无期
    2021-01-20 03:21

    Personally I often use aggregate initialisation for simple data objects:

    struct MyStruct {
        int x;
        int y;
        char a;
        char b;
    };
    
    int main()
    {
        MyStruct s = {10, 20, 'e', 'u'};
    }
    

    Or hard coded defaults like this:

    struct MyStruct {
        int x;
        int y;
        char a;
        char b;
    };
    
    const MyStruct MyDefault_A = {0, 0, 'a', 'z'};
    const MyStruct MyDefault_B = {10, 20, 'e', 'u'};
    
    int main()
    {
        MyStruct s = MyDefault_A;
    }
    

提交回复
热议问题