繁体   English   中英

我们如何使用结构?

[英]How do we use structs?

我遇到的主要问题是有这么多参数,这些参数我只是想摆脱掉,是的,我不理解结构的逻辑。 但是,它变得更加清晰了...

编辑所以cHao希望我使用一个特定的案例,所以这是我编写的一个示例:

#include <stdio.h>

int main()
{
    //Top coord. of the square
    int top_x1 = 0;
    int top_y1 = 10;
    int top_x2 = 10;
    int top_y2 = 10;

    //Bottom coord. of the square
    int bottom_x1 = 0;
    int bottom_y1 = 0;
    int bottom_x2 = 10;
    int bottom_y2 = 0;

    //Left coord. of the square
    int left_x1 = 0;
    int left_y1 = 0;
    int left_x2 = 0;
    int left_y2 = 10;

    //Right coord. of the square
    int right_x1 = 10;
    int right_y1 = 0;
    int right_x2 = 10;
    int right_y2 = 10;

    parameter(top_x1, top_y1, top_x2, top_y2, bottom_x1,
              bottom_y1, bottom_x2, bottom_y2, left_x1,
              left_y1, left_x2, left_y2, right_x1, right_y1,
              right_x2, right_y2);
}

parameter (int top_x1,int top_y1,int top_x2,int top_y2,int bottom_x1,
              int bottom_y1,int bottom_x2,int bottom_y2,int left_x1,
              int left_y1,int left_x2,int left_y2,int right_x1,int right_y1,
              int right_x2,int right_y2)
{
    int totalParameter, topSide, bottomSide, leftSide, rightSide;

    topSide = (top_x2 - top_x1);
    bottomSide = (bottom_x2 - bottom_x1);
    leftSide = (left_y2 - left_y1);
    rightSide = (right_y2 - right_y1);

    totalParameter = (topSide + bottomSide + leftSide + rightSide);
    printf("%d\n", totalParameter);

}

如果我尝试使用结构...

#include <stdio.h>>

struct coordinates
{
    int x1, y1, x2, y2;
};

int main()
{
    struct coordinates top;
    struct coordinates bottom;
    struct coordinates left;
    struct coordinated right;

    //Top line of the square
    top.x1 = 0;
    top.y1 = 10;
    top.x2 = 10;
    top.y2 = 10;

    //Bottom line of the square
    bottom.x1 = 0;
    bottom.y1 = 0;
    bottom.x2 = 10;
    bottom.y2 = 0;

    //Left line of the square
    left.x1 = 0;
    left.y1 = 0;
    left.x2 = 0;
    left.y2 = 10;

    //Right line of the square
    right.x1 = 10;
    right.y1 = 0;
    right.x2 = 10;
    right.y2 = 10;
}

parameter(top, bottom, left, right)
{
    int totalParameter, topSide, bottomSide, leftSide, rightSide;

    topSide = (top.x2 - top.x1);
    bottomSide = (bottom.x2 - bottom.x1);
    leftSide = (left.y2 - left.y1);
    rightSide = (right.y2 - right.y1);

    totalParameter = topSide + bottomSide + leftSide + rightSide;
    printf("%d\n", totalParameter);

}

不管用,有什么帮助吗? :P我得到的错误是:“请求成员'x1'的方式不是联合结构。对于所有x和y坐标。

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);
}

指的是struct的一般用法,而不是技术方面:(因为在我看来您不了解其中的逻辑)

您应该将struct用作一组变量的一部分。

例如,结构点(表示空间中的点)将包含X的int和Y的int。

您应该将数组用于变量组,它们之间的关系是串行的。

例如:班上的学生,因为您要对每个学生依次执行相同的操作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM