【问题标题】:Accessing struct array elements访问结构体数组元素
【发布时间】:2013-04-19 17:37:37
【问题描述】:

这个程序应该将用户的输入捕获到结构中,然后打印出给定信息的直方图。到目前为止一切正常,除了当我尝试打印直方图时,无论学生的成绩如何,所有的“*”字符都属于 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("*");
        }

    }
}

【问题讨论】:

    标签: c arrays struct


    【解决方案1】:
    displayHist(&studentRecords[students], students);
    

    &amp;studentRecords[students] 是数组studentRecords 之后的地址。在displayHists 中,对records[i] 的访问将尝试取消引用studentRecords[students+i],这超出了数组的范围。

    正确的调用可能是:

    displayHist(&studentRecords[0], students);
    

    相当于:

    displayHist(studentRecords, students);
    

    顺便说一句,scanfchar * 中不需要使用&amp;,因为char (*)[]char * 可能有不同的内存表示。

    【讨论】:

    • 啊啊啊!大声笑谢谢我应该知道的。我正在为那个错误拍自己的头大声笑。非常感谢
    【解决方案2】:
    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]
    

    当您使用地址时,即&amp;,它变为char **,这不是scanf 所期望的。

    所以尝试使用这种方式。

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

    displayHist(studentRecords, students);
    

    【讨论】:

    • 注意它不会变成char **;它变成char (*)[28](指向28 个字符的数组的指针),与错误消息所述完全一样。你说得对,这不是scanf() 所期望的(但奇怪的是,传递的地址是相同的)。
    猜你喜欢
    • 1970-01-01
    • 2021-09-20
    • 1970-01-01
    • 2013-06-11
    • 2013-10-21
    • 2018-05-27
    • 2019-08-10
    • 2016-06-19
    • 1970-01-01
    相关资源
    最近更新 更多