c - 如何使用结构体?

标签 c struct

提出一个新问题,因为我的旧问题在花了很长时间编辑后就死了。 所以我试图摆脱这段代码中的所有这些参数(是的,有些参数是多余的,但我只是添加了它们以防万一):

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

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

}

我希望它打印出来:40。

还是不行,请问有什么帮助吗? 我得到的错误是:“请求成员‘x1’不是 union 结构。 对于所有 x 和 y 坐标。

最佳答案

该函数需要定义其返回类型并定义其参数类型,如下所示:

void parameter(struct coordinates top, struct coordinates bottom, struct coordinates left, struct coordinates right)

如果不这样做,参数默认为 int 类型(这更像是 C 语言的历史课)。如果您也不向前声明该函数,那么它就是 implicitly declared (这在现代 C 中是非法的)并假设您的参数和函数类型与应有的不同(它们都默认为 int 类型)。

关于c - 如何使用结构体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20168460/

相关文章:

c - MISRA C 2012 规则 15.4 用于终止任何迭代语句的break或goto语句不应超过一个

c - 错误 !空列表

c - 这是从函数返回结构数组的正确方法吗?

nhibernate - 将不可变结构映射为 NHibernate 中的组件

c malloc 指向结构数组的指针

dictionary - 无法增加 map 中结构的值

c - 数组 typedef 结构在被函数调用时无法正常工作

在动态数组之间移动结构的 C 方法

c - 哪个套接字,clientSocket = accept() 或 listen(socket),你设置了 sockopt SO_KEEPALIVE?

c - 预设列表中的随机数