开发者

scanf to struct doesnt work

开发者 https://www.devze.com 2023-03-10 09:50 出处:网络
I\'m having this function and I need to get coordinates to a structure. These are the structures: //---------STRUCTURES----------

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;

and this is the function:

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 th开发者_JAVA百科e 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;
}

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

any suggestions?


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

should be

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

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消