Well, I Have 1 struct called Student and 2 called Students. Struct Students has an Array of structs Student. When i pass the second struct after giving the id, name and surname, the program crushes. What is wrong with the code?
Code:
#include <stdio.h>
#include <stdlib.h>
typedef struct Student{
int id;
char name[20];
char surname[20];
} Student;
typedef struct Students{
Student *arr;
int size;
int sorted; //1 if by surname, name, 2 if by ID
} Students;
int main(int argc, char *argv[]){
Students st;
Student st1,st2;
st.size=0;
st.arr = (Student *) malloc (sizeof(Student));
printf("Add Student's Information:\n");
printf("ID: ");
scanf("%d", &(st1.id));
printf("Name: ");
scanf("%s", &(st1.name));
printf("Surname: ");
scanf("%s", &(st1.surname));
st.arr[st.size]=st1;
(st.size) = (st.size) + 1;
st.arr = (Student *) realloc (st.arr, sizeof(Student));
printf("Add Student's Information:\n");
printf("ID: ");
scanf("%d", &(st2.id));
printf("Name: ");
scanf("%s", &(st2.name));
printf("Surname: ");
scanf("%s", &(st2.surname));
st.arr[st.size]=st2;
printf("%d",st.size);
(st.size) = (st.size) + 1;
system ("PAUSE");
}
Something may be wrong with memory allocation but don't know how to achieve the allocation for the st.arr[1]