How do we use structs?

后端 未结 2 1415
离开以前
离开以前 2021-01-25 18:15

The main problem I\'m having is having so many parameters, which I just want to get rid of, and yes I do not understand the logic of structs. However, it is becoming a bit more

2条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-25 18:59

    typedef struct _foo
    {
        int x1, x2, x3,..., x20;
    } foo;
    
    int add(const foo *pBar)
    {
        return pBar->x1 + pBar->x2 + pBar->x3 + ... + pBar->x20;
    }
    
    int main()
    {
        // declare and initialize the struct
        foo bar = { 1, 2, 3, ..., 20 };
    
        // an alternative way of initializing the struct:
        bar.x1 = 1;
        bar.x2 = 2;
        bar.x3 = 3;
         :
        bar.x20 = 20;
    
        int total = add(&bar);
    }
    

提交回复
热议问题