scanf 到 struct 不起作用 [英] scanf to struct doesnt work

查看:44
本文介绍了scanf 到 struct 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个功能,我需要获取结构的坐标.这些是结构:
//---------结构----------

I'm having this function and I need to get coordinates to a structure. These are the structures:
//---------STRUCTURES----------

typedef struct coordinates  
{
    int x_l;
    int y_l;
    int x_r;
    int y_r;
} Coordinates;

typedef struct field 
{
    char Id;
    Coordinates location;
    int area;
    int price;
} Field;

这是函数:

Field GetFieldFromUser(int cptr,Field *pf1)
{       
    //Field *pf
    int i=0;

    printf("\nPlease enter information for your new field:\n");
    printf("the Id of the field is 5 digit num between 00000-99999:\n");
    printf("The Id of the Field given Automatic by the system\n");


    pf1->Id= 0000+cptr;

    printf("\nThis is the ID for the new field: ");

    printf("id = %05d\n",pf1->Id);

    printf("\nPlease enter the coordinates of the top left of the field (press ' ' between the digits)\n");
    scanf("%d %d",pf1->location.x_l,pf1->location.y_l);
    fflush(stdin);
    printf("Please enter the coordinates of the lower right of the field (press ' ' between the digits)\n");
    scanf("%d %d",pf1->location.x_r,pf1->location.y_r);

    return *pf1;
}

现在在编译器把我扔出去的位置的scanf,我不知道为什么

now at the scanf of the location the compiler throw me out, and i dont know why

有什么建议吗?

推荐答案

scanf("%d %d",pf1->location.x_l,pf1->location.y_l);

应该是

scanf("%d %d",&(pf1->location.x_l), &(pf1->location.y_l));

下一个 scanf 的逻辑相同.scanf 期望它可以写入的地址.您正在传递它的值(可能是未初始化的值).所以它试图写入一些未知的内存位置.

same logic for the next scanf. scanf expects addresses that it can write to. You were passing it values (uninitialized values probably). So it was trying to write to some unknown memory location.

这篇关于scanf 到 struct 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆