【问题标题】:Student Struct Troubles学生结构问题
【发布时间】:2015-09-02 04:23:15
【问题描述】:

我正在学习structs,但我仍然对它们以及它们的作用感到困惑。我拥有的代码是我的尝试,我不断收到分段错误。我的主要目标是让用户在调用函数时查看他们想要添加多少学生,以及姓名和分数的每个信息。我还想将数据数组打印给用户。

  • 对于loadStudentData() 函数,我想将newNameNewScore 存储到newStudent 结构中。

  • 对于printStudentData(),我想为单个学生打印数据

  • 然后对于 printStudentArray(),我想在学生数组的每个成员上调用 printStudentData() 函数。

我的代码是:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define STUDENT_NAME_LENGTH 20

typedef struct{
char* name;
int score;
}Student;

void loadStudentData(Student *newStudent,char* newName,int newScore);
int printStudentData(Student student);
void printStudentArray(Student* students, int numStudents);


/*
 *
 */
int main(int argc, char** argv) {
int numberOfStudents;
Student *newStudent;
char* newName;
int newScore;
int student;


Student students[numberOfStudents];

printf("how many students: ");
scanf("%d", &numberOfStudents);


    loadStudentData(newStudent, newName, newScore);

    printStudentArray(students, numberOfStudents);


  return (EXIT_SUCCESS);
}

void loadStudentData( Student *newStudent, char* newName, int newScore){
 int i;
    char *studentName = (char *) malloc(STUDENT_NAME_LENGTH *  sizeof(char));
    scanf("%s", studentName);
    newStudent->name = studentName;

    int nScore;
    scanf("%d", &nScore);
    newStudent[i].score = newScore;
  }
 int printStudentData(Student student){

int i;

    printf("Student name\t%s\n",) ;
    printf("Student score\t%d\n",);

     }



 void printStudentArray(Student* students, int numStudents){
 int i;
for (i = 0; i < numStudents; i++) {
    printf("Student name\t%s\n", students[i].name);

    printf("Student score\t%d\n", students[i].score);
   } 
   }

【问题讨论】:

    标签: c arrays function pointers struct


    【解决方案1】:

    在实际初始化结构数组之前调用printStudentArray。这意味着数组中的所有数据都将是不确定的,并且以任何方式使用它,除了初始化它会导致未定义的行为(这是崩溃的常见原因)。

    更具体地说,问题可能出在您尝试打印名称时,因为name 指针可能指向任何地方

    【讨论】:

      【解决方案2】:
      • 第 1 点: Student students[numberOfStudents]; 无效,因为 numberOfStudents 未初始化。

      • 第 2 点:在您的代码中,您在填充 student 本身之前调用了 printStudentArray()

        此函数尝试访问name 变量处的未初始化内存,这会导致undefined behaviour

      • 第 3 点:在您的 loadStudentData() 函数中,您使用的是 i uninitialized 。再次UB。

      也就是说,

      1. 在使用重新调整的指针之前,始终检查 malloc() 是否成功。
      2. do not castmalloc()C中的family返回值。

      【讨论】:

      • student[numberofstudents],永远不会起作用,因为 numberofstudents 需要在编译时确定。 OP 需要 malloc/calloc 学生数组,然后在退出前释放它。
      • @rhubarbdog 是吗?我很感兴趣。请告诉我怎么做。 (FWIW,我提到了this)。这就是反对票的原因吗? :-)
      • C 没有 wiki 讨论的可变长度数组。目前,该语句所做的只是创建一个由零元素组成的固定长度数组,因此您可以不存储任何内容。在 c 中,您可以测量,分配空间使用它并释放它。或者从一些东西开始,开始填充增长(realloc)和填充,如果需要,让它变小,然后使用,最后在完成时使用。我不能强调你必须释放你通过 calloc、malloc 或 realloc 获得的所有内存,否则它是内存泄漏
      • C doesn't have variable length arrays like that wiki discusses... 你也考虑过C99 吗? :-)
      • C 是语言。像 C99 和 C11 这样的东西是标准的。第 4 点...永远不要转换 malloc 返回的地址,请在 SO 上查找理由
      【解决方案3】:

      您似乎没有分配您正在创建的结构。请注意,当您声明诸如 Student *newStudent; 之类的变量时,您首先必须分配它。因为到目前为止,您只声明了一个变量,该变量包含一个指向Student 结构的指针,但该指针不指向任何地方。因此,在您期望有效指针的任何地方都使用 malloc 函数:

      newStudent = malloc(sizeof(newStudent));

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-03-27
        相关资源
        最近更新 更多