指向结构的指针

use*_*010 2 c pointers structure

我是指针的新手,并尝试使用指向结构的指针.但在第一次进入后,我的程序崩溃了.请帮助我.

这是结构定义:

struct students{//structure students definition
   char name[20];
   char RegNo[15];
   char CourseUnit[10];
   int score;
   char grade;
};
Run Code Online (Sandbox Code Playgroud)

等级不应由用户输入,而是由程序计算.

到目前为止,这是我写的代码:

int main()//start of main
{
    struct students *myStudPtr,myStud[SIZE];

    myStudPtr=&myStud[SIZE];

    int i;//declare variables
    int count;

    printf("How many students do you want to deal with?\n");
    scanf("%d",&i);//number of entries

    for(count=1;count<=i;count++) {
        printf("Enter student's name\n");
        scanf("%s",&(*myStudPtr).name);

        printf("Enter the student's registration number\n");
        scanf("%s",&(*myStudPtr).RegNo);

        printf("Enter the course unit\n");
        scanf("%s",&(*myStudPtr).CourseUnit);

        printf("Enter the marks scored\n");
        scanf("%d",&(*myStudPtr).score);
    }

    printf("NAME\tREGISTRATION\t\tCOURSE UNIT\tSCORE\t\tGRADE\n");//tabulates the output
    printf("%s\t", myStudPtr->name);
    printf("%s\t\t", myStudPtr->RegNo);
    printf("%s\t", myStudPtr->CourseUnit);
    printf("%d\t\t", myStudPtr->score);

    if(myStudPtr->score>100) {
        printf("Invalid\n");
    } else if(myStudPtr->score<40) {
        printf("FAIL\n");
    } else if(myStudPtr->score<50) {
        printf("D\n");
    } else if(myStudPtr->score<60) {
        printf("C\n");
    } else if(myStudPtr->score<70) {
        printf("B\n");
    } else if(myStudPtr->score>70) {
        printf("A\n");
    } else {
        printf("Invalid");
    }

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

请帮助.谢谢.

Gri*_*han 9

您有一个索引越界错误,导致运行时未定义的行为:

myStudPtr = &myStud[SIZE];
//                  ^^^^^ 
//                  wrong 
Run Code Online (Sandbox Code Playgroud)

根据声明struct students myStud[SIZE];,最大指数值可以SIZE - 1.记住数组索引以0.

编辑

据我所知,你已经声明了一个struct数组,并希望i使用指向struct的指针从用户那里读取学生信息的数量.但是你的代码中还有一些问题,例如在for循环中你总是访问相同的 struct元素:

for(count = 1; count <= i; count++)
    scanf("%s", &(*myStudPtr).name);
//                  ^   
//                  points to same struct element in array
Run Code Online (Sandbox Code Playgroud)

每个scanf()printf()陈述中都存在这个错误.

正确如下:

  1. 初始化指向数组中第一个元素地址的指针:

     myStudPtr = &myStud[0];
     //                  ^ first element at 0th index 
    
    Run Code Online (Sandbox Code Playgroud)
  2. 通过指针,您可以通过以下两种方式之一访问struct的元素:

    第一:例如扫描score学生的价值:

    scanf("%d", &myStudPtr[count].score);
    
    Run Code Online (Sandbox Code Playgroud)

    注: 优先级[]"数组下标运算符"是高于."通过对象名运营商成员选择",所以你不需要()括号..运算符的优先级也高于&&符号运算符,因此即使您不需要任何括号来获取地址(例如,&(myStudPtr[count].score)不需要).

    第二:score使用指针和->操作符扫描学生的值:

    scanf("%d", &(myStudPtr + count)->score);
    
    Run Code Online (Sandbox Code Playgroud)

    注意:+plus运算符的优先级较低,因此我们需要括号来覆盖优先级.->成员选择的优先级通过指针运算符高于&&符运算符,所以这里你也不需要任何括号&((myStudPtr + count)->score).

重要说明:

  1. 您应该检查i用户输入的值必须小于SIZE(strcut数组的大小),否则您的代码中可能有未定义的行为.

  2. 读取字符串使用安全fgets()函数而不是scanf以避免缓冲区溢出.阅读:"用scanf()不好的方式读一条线?"

还有一点注意事项:

  1. 如你是新C程序员(我觉得)你应该阅读:缩进C程序的快速教程学习实践压痕.

  2. 你应该始终保持空间后,,并;让你的代码的可读性,出于同样的原因类似的表达count<=i 应写为count <= i.比较你的for循环:

     for(count=1;count<=i;count++)
    
    Run Code Online (Sandbox Code Playgroud)

    接下来我想建议你:

     for(count = 1; count <= i; count++){
         // code after one tab
     }  
    
    Run Code Online (Sandbox Code Playgroud)

改进代码:

我还建议你改进if-else编码风格,你的if-else部分代码可以写成如下:

score = myStudPtr->score;
if(0 <= score && score <= 100){
  if(score > 70)
    printf("A");
  if(60 <= score && score < 69)
    printf("B");    
  if(50 <= score && score < 59)
    printf("C");    
  if(40 <= score && score < 49)
    printf("D");    
  if(score < 40)
    printf("FAIL");
}
else
 printf("Error: Invalid Entry!");
printf("\n");
Run Code Online (Sandbox Code Playgroud)
  • 我删除了很多else语句,而是使用了&&.
  • 删除多余的{..}支撑对.
  • 使用局部score变量myStudPtr->score来保持代码看起来简单.
  • \n从每个pritf语句中删除而不是printf在最后添加一个新的(不是很重要).

最后一个错误

要打印每个学生记录,您需要一个新的循环来调用printf()函数,并包含if-else逻辑来评估学生成绩.