2013-04-19 21 views
5

Questo programma dovrebbe acquisire l'input dell'utente nella struttura e quindi stampare un istogramma delle informazioni fornite. Tutto funziona bene finora tranne che quando provo a stampare l'istogramma tutti i caratteri '*' cadono sotto il voto F, a prescindere dal voto dello studente. Quello che penso stia succedendo è che l'indice dell'array dello studente viene passato al posto della variabile effettiva stessa, ma sono confuso perché nell'output prima che l'istogramma venga stampato mostra il valore corretto. Eventuali suggerimenti?Accesso agli elementi della matrice struct

#include <stdio.h> 
#include <stdlib.h> 
#define MAXSIZE 28 
#define MAXGRADE 100 

struct studentData{ 
    char studentID[MAXSIZE]; 
    char studentName[MAXSIZE]; 
    int examPercent; 
} studentRecords[MAXSIZE]; 

// function prototype for histogram 
void displayHist(struct studentData *records, int classSize); 

int main() 
{ 
    int i, students = -1; 
    //struct studentData *studentRecords[MAXSIZE]; 
    while(students < 0 || students > MAXSIZE) 
    { 
     printf("Please enter the number of students in your class:\n"); 
     scanf("%d", &students); 

     if(students > MAXSIZE || students <= 0) 
     { 
      printf("Try again..\n"); 
      scanf("%d", &students); 
     } 

    } 

for(i=0;i<students;i++) { 

     printf("Please enter the student #%d's lastname:\n", i+1); 
     scanf("%s", &studentRecords[i].studentID); 


     printf("Please enter the student #%d's ID#:\n", i+1); 
     scanf("%s", &studentRecords[i].studentName); 

     printf("Please enter the student's exam percent:\n"); 
     scanf("%d", &studentRecords[i].examPercent); 

} 

//This is just here to view the input... 
for(i=0;i<students;i++) { 

     printf("Student #%d's name is %s\n", i+1, studentRecords[i].studentName); 
     printf("student #%d's ID#:%s\n", i+1, studentRecords[i].studentID); 
     printf("student #%d's grade was %d\n", i+1, studentRecords[i].examPercent); 

    } 

    displayHist(&studentRecords[students], students); 

    return 0; 
} 

void displayHist(struct studentData *records, int classSize) 
{ 
    int i; 


     printf("A:"); 
    for(i=0;i<classSize;i++) 
    { 

     if(records[i].examPercent >=90) 
     { 
      printf("*"); 
     } 

    } 


     printf("\n"); 
     printf("B:"); 
    for(i=0;i<classSize;i++) 
    { 
     if(records[i].examPercent< 90 && records[i].examPercent >= 80) 
     { 
      printf("*"); 
     } 
    } 

     printf("\n"); 
     printf("C:"); 
    for(i=0;i<classSize;i++) 
    { 
     if(records[i].examPercent < 80 && records[i].examPercent >= 70) 
     { 
      printf("*"); 
     } 

    } 

    printf("\n"); 
    printf("D:"); 
    for(i=0;i<classSize;i++) 
    { 
     if(records[i].examPercent< 70 && records[i].examPercent >= 60) 
     { 
      printf("*"); 
     } 

    } 

    printf("\n"); 
    printf("F:"); 
    for(i=0;i<classSize;i++) 
    { 
     if(records[i].examPercent < 60) 
     { 
      printf("*"); 
     } 

    } 
} 

risposta

2
displayHist(&studentRecords[students], students); 

&studentRecords[students] è un indirizzo dopo l'array studentRecords. In displayHists, gli accessi a records[i] tenteranno di dereferenziare studentRecords[students+i], che è fuori dai limiti dell'array.

Una chiamata corretta potrebbe essere:

displayHist(&studentRecords[0], students); 

che è equivalente a:

displayHist(studentRecords, students); 

Tra l'altro, non c'è bisogno di utilizzare & in scanf con char *, perché char (*)[] e char * possono avere diverse rappresentazioni di memoria.

+1

Aaaa! Lol, grazie avrei dovuto saperlo. Mi sto prendendo a schiaffi in testa per quell'errore. Grazie mille – KryptNick

0
scanf("%s", &studentRecords[i].studentID); 

scanf("%s", &studentRecords[i].studentName); 

warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[28]’ [-Wformat] 

Quando si utilizza l'indirizzo-di, vale a dire &, diventa char **, che non è quello che si aspetta scanf.

Quindi provare a utilizzare in questo modo.

scanf("%s", &(*studentRecords[i].studentID)); 

e

displayHist(studentRecords, students); 
+0

Nota che non diventa 'char **'; diventa 'char (*) [28]' (un puntatore a un array di 28 caratteri), esattamente come afferma il messaggio di errore. Hai ragione che non è ciò che 'scanf()' si aspetta (ma, curiosamente, l'indirizzo passato è lo stesso). –

Problemi correlati