简体   繁体   English

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

[英]Initializing Strings in an Array of Sturts within a Struct

I have a struct gradebook with(among other things) an array of student structs that has two string fields 我有一本struct成绩簿,其中有(其中包括)一个带有两个字符串字段的学生结构数组

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

I have an initialization function 我有一个初始化功能

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

}

and I need to then insert, into those two string fields, the passed in strings, with my add_student function. 然后我需要使用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;
}

My code compiles, but when I run it with the main function, I get a seg fault. 我的代码可以编译,但是当我使用main函数运行它时,会出现段错误。 The add_student function is what I am ultimately trying to do (copy the given string into book->student[ndx].name) If you need to see the main file or the gradebook.h file, let me know. 我最终将尝试执行add_student函数(将给定的字符串复制到book-> student [ndx] .name中)。如果需要查看主文件或gradebook.h文件,请告诉我。

Edit: Thanks to all of you, this issue has been solved. 编辑:谢谢大家,这个问题已经解决。 The main problem, as abginfo pointed out, was my If Else + the For loop inside of it. 如abginfo所指出的,主要问题是我的If Else +其中的For循环。 But now I have other problems further along in my program. 但是现在我的程序中还有其他问题。 Haha, Thank You. 哈哈谢谢你。

From what portion of your code I can see, I'm going to make the assumption that the init_gradebook function takes a non allocated reference to gradebook and attempts to initialize it. 从代码的哪一部分看,我将假设init_gradebook函数采用未分配的对成绩簿的引用并尝试对其进行初始化。 In this case the gradebook reference you have has no memory allocated to it just yet. 在这种情况下,您尚未为成绩簿参考分配任何内存。 Try using the malloc() function to assign the required memory to your gradebook reference before attempting to initialize the rest of its variables. 在尝试初始化其其余变量之前,请尝试使用malloc()函数将所需的内存分配给您的成绩簿引用。

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

I've changed the variable name to avoid any confusion. 我已经更改了变量名以避免任何混淆。

To supplement varevarao's answer, you should allocate everything explicitly as a matter of habit instead of relying on segfaults to tell you something's not allocated. 为了补充varevarao的答案,您应该习惯性地明确分配所有内容,而不要依靠段错误来告诉您未分配的内容。 (Not that you necessarily do!) Messing with unallocated memory is undefined behavior, so in some cases this code does not trigger an error - (并非您一定要这样做!)处理未分配的内存是未定义的行为,因此在某些情况下,此代码不会触发错误-

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

returned (on my machine) 返回(在我的机器上)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM