Inserting new member into array of structs
This is the 2nd time I have attempted this subject, and am still confused :confused:
I need to create an array of structs. I think I can do this. I then need to insert into the array - this is where I am having problems.
This is my struct in my header file:
In the main file, I have 6 operations to perform , so I have used a switch case. I have provided code for the first 2 operations - 0 is to end the program (I think I have that one worked out!!), and case 1 is to add a new student - this is where my problem is.
typedef struct student *Student;
In create_student I have attempted to create a new struct containing the ID of the student. The second part of the struct points to a Binary Search Tree, which I have initialised to NULL at this point - I am not even going there yet!! Here is the code for create_student:
int action; /* tells the program which operation to perform */
int ID; /* to scan the student's ID */
struct student students; /* sets up the array */
Student *ptr_to_array; /* points to where the array starts*/
ptr_to_array = &students; /* assumes it is an empty array */
int e; /* to hold the value of the end of the array */
printf("entered case 0\n");
printf("Ready to scan student ID\n");
current_student = create_student(ID);
This generates a compiler warning: "assignment from incompatible pointer type".
Student create_student(int n)
Student new_student = (Student) malloc(sizeof(struct student));
Secondly, here is the code for adding student. I have attempted to add the student into the array at the end, and then compare the ID value with the previous one in the array. If it is less, swap them etc in order to keep it in sortred order.
This gives the compiler warning: "passing arg 2 of 'add_student' from incompatible pointer type."
void add_student(Student a, Student c, int e)
a[e] = c; /* put the created student into the end of the array */
for (i = e-1; i=0; i--)
&previous = a[e-1];
&temp = &c;
&c = &previous;
&previous = &temp;
Notwithstanding the errors in the swapping of the values, my main question is:
Why am I getting these warnigns about the pointer types being incompatible?
Please please help as I am tearing my hair out.