2013-04-19 56 views
5

Chương trình này được cho là bắt đầu nhập từ người dùng vào cấu trúc và sau đó in ra một biểu đồ của thông tin đã cho. Tất cả mọi thứ hoạt động tốt cho đến nay ngoại trừ khi tôi cố gắng in biểu đồ tất cả các ký tự '*' thuộc lớp F bất kể lớp của học sinh là gì. Những gì tôi nghĩ đang xảy ra là chỉ số mảng của học sinh đang được chuyển thay vì biến thực tế, nhưng tôi bị nhầm lẫn bởi vì trong đầu ra trước khi biểu đồ được in nó cho thấy giá trị chính xác. Bất kỳ đề xuất?Truy cập các phần tử cấu trúc mảng

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

    } 
} 

Trả lời

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

&studentRecords[students] là một địa chỉ sau mảng của bạn studentRecords. Trong displayHists, quyền truy cập vào records[i] sẽ cố gắng tham khảo studentRecords[students+i], nằm ngoài giới hạn của mảng của bạn.

Một cuộc gọi đúng có thể là:

displayHist(&studentRecords[0], students); 

Đó là tương đương với:

displayHist(studentRecords, students); 

Bằng cách này, không cần phải sử dụng & trong scanf với char *, vì char (*)[]char * có thể khác nhau biểu diễn bộ nhớ.

+1

Aaaa! Lol cảm ơn tôi nên biết điều đó. Tôi tát bản thân mình vào đầu cho sai lầm đó LoL. Cảm ơn rất nhiều – 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] 

Khi bạn sử dụng địa chỉ-of, ví dụ: &, nó trở nên char **, mà không phải là những gì scanf hy vọng.

Vì vậy, hãy thử sử dụng cách này.

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

displayHist(studentRecords, students); 
+0

Lưu ý rằng nó không trở thành 'char **'; nó trở thành 'char (*) [28]' (một con trỏ tới một mảng gồm 28 ký tự), chính xác như thông báo lỗi nói rõ. Bạn nói đúng rằng nó không phải là những gì 'scanf()' mong đợi (nhưng, tò mò đủ, địa chỉ được thông qua là như nhau). –

Các vấn đề liên quan