2011-06-03 43 views
2

我有這個功能,我需要得到一個結構的座標。 這些是結構:
// --------- ----------結構scanf結構不工作

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,我不知道爲什麼

有什麼建議嗎?

回答

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

應該是

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

用於下一scanf的相同的邏輯。 scanf需要它可以寫入的地址。你正在傳遞它的值(可能是未初始化的值)。所以它試圖寫入一些未知的內存位置。

+0

好極了!謝謝你...我忘了&... – talmordaniel 2011-06-03 14:28:01

+1

不需要括號 – ergosys 2011-06-03 15:52:59