Good evening/morning/day/afternoon, everyone. I am presently having a bit of an issue with my code. I am trying to make an array of structs called student, and am having issues figuring out how to point from one to the next in the array. Any guidance on the matter would be much appreciated.
main.c
int main()
{
int n_students = 0;
struct student students[1000];
int closebool = 0;
int IDnum;
int k;
char buffer[101];
char *studentName_tmp;
do
{
scanf("%d", &operation_num);
switch (operation_num)
{
case 0 :
{
closebool = 1;
break;
}
case 1 :
{
scanf("%d %s", &IDnum, buffer);
studentName_tmp = (char *) malloc (strlen(buffer)+1);
strcpy (studentName_tmp, buffer);
n_students = insert(students, n_students, IDnum, studentName_tmp);
printf ("%d %s\n", students[n_students].ID, students[n_students].name);
n_students++;
break;
}
}
} while (closebool != 1);
return 0;
}
student.c
int insert(struct student array[], int numberof_students, int IDnum, char *student_name)
{
array[numberof_students].ID = IDnum;
array[numberof_students].name = student_name;
return 0;
}
This is the input and output I expect to see: (input input output, in this case)
1, 123 fred, 123 fred, 1, 234 george, 234 george, 1, 345 henry, 345 henry,
However I see this:
1, 123 fred, 123 fred, 1, 234 george, 123 fred, 1, 345 henry, 123 fred,