gpt4 book ai didi

c - 指向结构的指针

转载 作者:行者123 更新时间:2023-12-04 11:34:25 25 4
gpt4 key购买 nike

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

结构定义如下:

struct students{//structure students definition
char name[20];
char RegNo[15];
char CourseUnit[10];
int score;
char grade;
};

成绩不应由用户输入,而应由程序计算。

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

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;
}

请协助。提前致谢。

最佳答案

你有一个 index-out-of bound导致的错误 undefined behavior在运行时:

myStudPtr = &myStud[SIZE];
// ^^^^^
// wrong

根据申报struct students myStud[SIZE]; , 最大索引值可以是 SIZE - 1 .记住数组索引以 0 开头.

编辑

据我了解,您已经声明了一个结构数组,并且想要读取 i使用指向结构的指针从用户获取的学生信息的数量。但是您的代码中还有更多问题,例如在 for 循环中,您始终访问相同 结构元素:

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

这个错误存在于每个 scanf() 中和 printf()声明。

正确的会是这样:

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

     myStudPtr = &myStud[0];
    // ^ first element at 0th index
  2. 通过指针,您可以通过以下两种方式之一简单地访问结构的元素:

    首先:例如扫描score学生的值(value):

    scanf("%d", &myStudPtr[count].score);

    注意: precedence[] 'array subscript operator' 高于 . '通过对象名称运算符选择成员',因此您不需要 ()插入语。同样优先于 .运算符高于 &符号运算符,因此即使您不需要任何括号来获取地址(例如不需要 &(myStudPtr[count].score))。

    第二:扫描score使用指针和 -> 的学生的值(value)运算符(operator):

    scanf("%d", &(myStudPtr + count)->score);

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

重要笔记:

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

  2. 读取字符串使用安全 fgets()函数而不是 scanf 以避免缓冲区溢出。阅读:"Reading a line using scanf() not good?"

再补充一点:

  1. 作为新的 C 程序员(我觉得)你应该阅读:Indenting C Programs它是学习缩进练习的快速教程。

  2. 您应该始终在, 之后保留空间和 ;使您的代码可读,出于同样的原因,像 count<=i 这样的表达式应该写成count <= i .比较你的 for 循环:

     for(count=1;count<=i;count++)

    然后我想建议你:

     for(count = 1; count <= i; count++){
    // code after one tab
    }

改进代码:

我还想建议你改进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");
  • 我删除了很多 else语句,而是使用 &&。
  • 已删除冗余 {..}大括号对。
  • 使用本地 score变量改为 myStudPtr->score使代码看起来简单。
  • 删除 \n来自每个 pritf声明改为添加新的 printf最后(不是很重要)。

最后一个错误:

要打印每个学生的记录,您需要在其中调用一个新循环 printf()功能,包括 if-else 逻辑来评估学生成绩。

关于c - 指向结构的指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18254623/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com