关于 c:如何在结构数组中为 typedef 结构分配内存


How to allocate memory to a typedef struct within array of structs

我一直在尝试为指针变量分配内存,但它一直给我错误或分段错误。如何正确初始化指向课程结构的指针变量?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
 typedef struct {
    char courseId[7];
    char courseName[10];
    } Course;

    struct Student{
    char firstName[10];
    char lastName[10];
    int studentId;
     Course *course;
    };

int main() {
  int numStudents;
  printf("How many students do you wish to register?:");
  scanf("%d", &numStudents);
  struct Student *student[numStudents];
  student[numStudents] = malloc(sizeof(*student[numStudents]));
  student[numStudents]->course = malloc(sizeof*(numStudents * Course));
  for (int i = 0; i < numStudents; i++) {
    printf("Enter student #%d First Name:", i+1);
    scanf("%s", student[i]->firstName);
    printf("Enter student Last Name:");
    scanf("%s", student[i]->lastName);
    printf("Enter Student ID:");
    scanf("%d", &student[i]->studentId);
    printf("Enter Course ID:");
    //student[i]->course = malloc(sizeof(*(student[i]->course)));
    scanf("%s", student[i]->course->courseId);
    printf("Enter Cousrse Name:");
    //student[i]->course = malloc(sizeof(*(student[i]->course)));
    scanf("%s", student[i]->course->courseName);
  }

  for (int i = 0; i < numStudents; i++) {
    printf("Student Name: %s %s\
"
, student[i]->firstName, student[i]->lastName);
    printf("Student ID: %d\
"
, student[i]->studentId);
    printf("Course Code: %s\
"
, student[i]->course->courseId );
    printf("Course name: %s\
"
, student[i]->course->courseName);
    free(student[i]->course);
  }


您几乎完成了,需要进行一些更正。
Student 中的课程对象不需要是指针。 Student 结构的数组需要动态分配,然后再释放。 -> 用作取消引用运算符,但在您的情况下, [] 已经取消引用,因此您将使用 . 而不是 -> 并且当您为结构数组分配内存时,它需要是结构的大小乘以结构的数量。 malloc(sizeof(struct Student)*numStudents);

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
typedef struct {
    char courseId[7];
    char courseName[10];
} Course;

struct Student{
    char firstName[10];
    char lastName[10];
    int studentId;
    Course course;
};

int main() {
    int numStudents;
    printf("How many students do you wish to register?:");
    scanf("%d", &numStudents);
    struct Student* students = malloc(sizeof(struct Student)*numStudents);
    for (int i = 0; i < numStudents; i++) {
        printf("Enter student #%d First Name:", i+1);
        scanf("%s", students[i].firstName);
        printf("Enter student Last Name:");
        scanf("%s", students[i].lastName);
        printf("Enter Student ID:");
        scanf("%d", &students[i].studentId);
        printf("Enter Course ID:");
        scanf("%s", students[i].course.courseId);
        printf("Enter Cousrse Name:");
        scanf("%s", students[i].course.courseName);
    }

    for (int i = 0; i < numStudents; i++) {
        printf("Student Name: %s %s\
"
, students[i].firstName, students[i].lastName);
        printf("Student ID: %d\
"
, students[i].studentId);
        printf("Course Code: %s\
"
, students[i].course.courseId );
        printf("Course name: %s\
"
, students[i].course.courseName);
    }
    free(students);
    return 0;
}

我试图对您的代码进行最小的更改。