初始化结构中Sturts数组中的字符串

时间:2013-12-13 19:47:33

标签: c

我有一个结构成绩册,其中包含(除其他外)一组学生结构,它有两个字符串字段

#define MAX_NAME_LEN 50
#define MAX_EMAIL_LEN 80
#define MAX_NUMBER_OF_STUDENTS 200
#define MAX_NUMBER_OF_ASSIGNMENTS 100

typedef struct students {
   char *name;
   char *email;
} Students;

typedef struct gradebook {
   int number_of_students;
   Students students[MAX_NUMBER_OF_STUDENTS];
   int number_of_assignments;
   char assignments[MAX_NUMBER_OF_ASSIGNMENTS][(MAX_NAME_LEN + 1)];
   int scores[MAX_NUMBER_OF_STUDENTS][MAX_NUMBER_OF_ASSIGNMENTS];
} Gradebook;

我有一个初始化函数

 int init_gradebook(Gradebook *book) {
   int row, col, ndx, count;

   book->number_of_students = 0;
   count += book->number_of_students;

   for(ndx = 0; ndx < MAX_NUMBER_OF_STUDENTS; ndx++) {
      book->students[ndx].name = 0;
      book->students[ndx].email = 0;
   }

   book->number_of_assignments = 0;
   count += book->number_of_assignments;

   for(row = 0; row < MAX_NUMBER_OF_ASSIGNMENTS; row++) {
      for(col = 0; col < (MAX_NAME_LEN + 1); col++) {
         book->assignments[row][col] = 0;
         count += book->assignments[row][col];
      }
   }

   for(row = 0; row < MAX_NUMBER_OF_STUDENTS; row++) {
      for(col = 0; col < MAX_NUMBER_OF_ASSIGNMENTS; col++) {
         book->scores[row][col] = 0;
         count += book->scores[row][col];
      }
   }

   if (count == 0) {
      return 1;
   } else {
      return 0;
   }

}

然后我需要使用add_student函数将传入的字符串插入到这两个字符串字段中。

int add_student(Gradebook *book, char *nom, char *mail) {
   int ndx, count;

   if (book->number_of_students == 0) {
      book->students[(book->number_of_students)].name = malloc(sizeof(51));
      book->students[(book->number_of_students)].email = malloc(sizeof(81));
      strcpy(book->students[(book->number_of_students)].name, nom);
      strcpy(book->students[(book->number_of_students)].email, mail);
      book->number_of_students++;
   } else {
   for (ndx = 0; ndx < book->number_of_students; ndx++) {
      book->students[(book->number_of_students)].name = malloc(sizeof(51));
      book->students[(book->number_of_students)].email = malloc(sizeof(81));
      strcpy(book->students[(book->number_of_students)].name, nom);
      strcpy(book->students[(book->number_of_students)].email, mail);
      book->number_of_students++;
      }
   }

   return 1;
}

我的代码编译,但是当我使用main函数运行它时,我得到一个seg错误。 add_student函数是我最终要做的(将给定的字符串复制到book-&gt; student [ndx] .name)如果你需要查看主文件或gradebook.h文件,请告诉我。

编辑:感谢大家,这个问题已经解决了。正如abginfo指出的那样,主要问题是我的If Else + For循环内部。但是现在我的程序中还有其他问题。哈哈,谢谢你。

2 个答案:

答案 0 :(得分:1)

从我可以看到的代码部分,我将假设init_gradebook函数采用未分配的成绩簿引用并尝试初始化它。 在这种情况下,您所拥有的成绩簿参考尚未分配给它。在尝试初始化其余变量之前,尝试使用malloc()函数为成绩簿参考指定所需的内存。

 gb = (Gradebook*)malloc(sizeof(*Gradebook));

我已更改变量名称以避免混淆。

答案 1 :(得分:0)

为了补充varevarao的答案,你应该明确地分配一切,而不是依赖段错误告诉你一些未分配的东西。 (不一定是你必须这样做!)使用未分配的内存进行混乱是未定义的行为,因此在某些情况下此代码不会触发错误 -

int main (void) {
  Gradebook mybook;
  init_gradebook(&mybook);
  printf("there are %i students\n", mybook.number_of_students);
  add_student(&mybook, "blerf", "blerf@gmail.com");

  printf("now there are %i students\n", mybook.number_of_students);
  printf("%s has an email address of %s\n", mybook.students[0].name, mybook.students[0].email);
  return 0;
}

返回(在我的机器上)

there are 0 students
now there are 1 students
blerf has an email address of blerf@gmail.com