我想将 struct 的引用添加到 struct 数组中.但我只能遵循该 struct ,并将该 struct 的副本设置为索引.有没有办法为学生的索引提供传递到Add_Student函数中的学生 struct 的地址?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
typedef struct student {
int age;
char *name;
} Student;
Student *students;
int size = 0;
void add_student(Student *student) {
Student *temp;
size++;
temp = realloc(students, sizeof(Student)*size);
if(!temp) {
printf("Cannot realloc memory\n");
exit(1);
}
temp[size-1] = *student;
students = temp;
}
int main(int argc, char **argv) {
students = malloc(0);
Student student;
student.name = "name1";
student.age = 11;
add_student(&student);
Student student2;
student2.name = "name2";
student2.age = 13;
add_student(&student2);
student2.age = 22;
student2.name = "name33";
// I change the student2 and i would except that the Student struct gets changed in students[1]
return 0;
}