【问题标题】:trouble trying to use a struct variable尝试使用结构变量时遇到问题
【发布时间】:2019-01-25 00:39:26
【问题描述】:

我正在学习 C 编程,我正在尝试编写一个简单的程序来存储 XY 平面中的点。

一开始我是这样做的:

#include<stdio.h>
#include<stdlib.h>
typedef struct point { float x, y; } PNT;
typedef struct plane
{
    int n_points;
    PNT pt[50];

} plane;
void points_input(plane planexy);
void points_distance(plane planexy);
int main()
{
    plane planexy;
    printf("How many points do you want to enter? : ");
    scanf("%d", &planexy.n_points);
    points_input(planexy);
    points_distance(planexy);
}
void points_input(plane planexy)
{
    int i;

    for (i = 0; i < planexy.n_points; i++)
    {
        printf("\nEnter a coordinate for x%d ", i);
        scanf("%f", &planexy.pt[i].x);
        printf("\nEnter a coordinate for y%d ", i);
        scanf("%f", &planexy.pt[i].y);
        system("cls");
    }
}
void points_distance(plane planexy)
{
    int i;
    printf("Select first point :\n");

    for (i = 0; i < planexy.n_points; i++)
    {
        printf("\n %d.(%.1f ,%.1f)", i, planexy.pt[i].x, planexy.pt[i].y);
    }
}

而且它不起作用,当我尝试访问某个点的某个变量时,它总是返回 0。

在弄乱了变量名称“planexy”并将其用作大小为 1 的向量,然后将所有提及它的内容都用作 planexy[0] 之后,它突然开始工作了:

#include<stdio.h>
#include<stdlib.h>
typedef struct point { float x, y; } PNT;
typedef struct plane
{
    int n_points;
    PNT pt[50];

} plane;
void points_input(plane planexy[]);
void points_distance(plane planexy[]);
int main()
{
    plane planexy[1];
    printf("How many points do you want to enter? : ");
    scanf("%d", &planexy[0].n_points);
    points_input(planexy);
    points_distance(planexy);
}
void points_input(plane planexy[])
{
    int i;

    for (i = 0; i < planexy[0].n_points; i++)
    {
        printf("\nEnter a coordinate for x%d ", i);
        scanf("%f", &planexy[0].pt[i].x);
        printf("\nEnter a coordinate for y%d ", i);
        scanf("%f", &planexy[0].pt[i].y);
        system("cls");
    }
}
void points_distance(plane planexy[])
{
    int i;
    printf("Select first point :\n");

    for (i = 0; i < planexy[0].n_points; i++)
    {
        printf("\n %d.(%.1f ,%.1f)", i, planexy[0].pt[i].x, planexy[0].pt[i].y);
    }
}

现在,这段代码可以工作了,但我不知道为什么我必须这样写才能让它发挥作用,而且它看起来不像是好的代码。

我知道如果我想使用多个平面“XY”我应该将变量存储为一个向量,但我只想使用一个,我不明白为什么当我使用大小为 1 的向量时它会起作用但是当我只想使用一个变量来存储一个平面时,它不会。

第一种方法有什么问题?

抱歉英语不好。

【问题讨论】:

  • 你的第一个代码不可能返回任何东西,因为它甚至没有编译。

标签: c struct


【解决方案1】:

当你编译这个时你从编译器得到的错误告诉你什么是错的:

program.c: In function ‘main’:
program.c:23:20: error: incompatible type for argument 1 of ‘ingreso_puntos’
     ingreso_puntos(planexy);
                    ^
program.c:15:6: note: expected ‘plane * {aka struct plane *}’ but argument is of type ‘plane {aka struct plane}’
 void ingreso_puntos(plane planexy[]);
      ^

您已将函数声明为使用指向平面的指针 (plane *),但您使用平面调用它们。使用&amp;运算符获取planexy的地址传递给函数。

program.c: In function ‘ingreso_puntos’:
program.c:31:28: error: request for member ‘n_puntos’ in something not a structure or union
     for (i = 0; i < planexy.n_puntos; i++)
                            ^

在函数中,planexy 是一个指向平面的指针,因此您需要 -&gt; 而不是 . 来访问其中的字段。

【讨论】:

    【解决方案2】:

    我已将您的问题编辑为英文,以便其他人可以用 SO 理解它。
    希望你能接受,我会用英文回答。

    您在函数points_input() 中传递对象planexy副本,因此main() 中定义的实际planexy 不会改变。相反,您应该传递一个指向它的指针,然后修改该指针指向的对象。

    修改后的代码:

    转发声明

    void points_input(plane* planexy); // ingreso_puntos function
    

    代码

    void points_input(plane* planexy)
    {
        int i;
    
        for (i = 0; i < planexy->n_points; i++)
        {
            printf("\nEnter a coordinate for x%d ", i);
            scanf("%f", &planexy->pt[i].x);
            printf("\nEnter a coordinate for y%d ", i);
            scanf("%f", &planexy->pt[i].y);
            system("cls");
        }
    }
    

    使用指针时,请使用 -&gt; 而不是点 .,后者仅用于对象。

    为什么它使用数组作为您的第二个代码?

    简单地说,数组/向量本身就是实际的指针。

    证明: scanf("%f", &amp;planexy[0].pt[i].x);scanf("%f", &amp;planexy-&gt;pt[i].x); 相同。

    main()。您应该传递创建指针的planexy地址

    points_input(&planexy);
    

    提示:当你想通过函数修改一个对象时,你传递一个指向它的指针,就像你对scanf所做的那样。否则,当您只想读取一个对象时,您可以正常传递它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-17
      • 1970-01-01
      • 2018-09-07
      • 1970-01-01
      • 2012-10-10
      相关资源
      最近更新 更多