2011-06-03 7 views
2

J'ai cette fonction et j'ai besoin d'obtenir les coordonnées d'une structure. Ce sont les structures:
// --------- ---------- STRUCTURESscanf à struct ne fonctionne pas

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; 

et c'est la fonction:

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

maintenant à la scanf de l'emplacement le compilateur me jeter, et je ne sais pas pourquoi

des suggestions?

Répondre

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

devrait être

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

même logique pour la prochaine scanf. scanf attend des adresses auxquelles il peut écrire. Vous lui passiez des valeurs (valeurs non initialisées probablement). Donc, il essayait d'écrire dans un endroit de mémoire inconnue.

+0

très bien! merci ... j'ai oublié le & ... – talmordaniel

+1

pas besoin de parenthèses – ergosys