2013-04-19 26 views
5

このプログラムは、ユーザーからの入力を構造体に取り込み、指定された情報のヒストグラムを出力することが想定されています。私がヒストグラムを印刷しようとすると、すべての「*」文字が学生のグレードにかかわらずFグレードに該当することを除いて、すべてがうまく動作します。私が考えているのは、実際の変数自体の代わりに学生の配列インデックスが渡されているということですが、ヒストグラムが出力される前の出力で正しい値が表示されるため、混乱します。助言がありますか?構造体配列要素へのアクセス

#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("*"); 
     } 

    } 
} 

答えて

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

&studentRecords[students]あなたの配列studentRecords後のアドレスです。 displayHistsでは、records[i]にアクセスすると、studentRecords[students+i]を参照解除しようとします。これは、配列の範囲外です。

正しい呼び出しは次のようになります。

と同等です
displayHist(&studentRecords[0], students); 

:ところで

displayHist(studentRecords, students); 

char (*)[]char *が異なる持っている可能性があるため、char *scanf&を使用する必要はありませんメモリ表現。すなわち&

+1

AAAA! LOLのおかげで私はそれを知っていたはずです。私は間違いのLoLのために頭の上に自分自身を叩いています。ありがとうございます – 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] 

あなたはアドレスの使用、それはない何scanfを期待している、char **になります。

この方法で試してみてください。

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

displayHist(studentRecords, students); 
+0

'char **'にはなりません。エラーメッセージの状態とまったく同じ 'char(*)[28]'(28文字の配列へのポインタ)になります。 'scanf()'が期待しているものではないということは間違いありません(しかし、不思議なことに、渡されるアドレスは同じです)。 –

関連する問題