HI
This is the 2nd time I have attempted this subject, and am still 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.Code:typedef struct student *Student; struct student { int ID; node_ptr units; };
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:Code:int action; /* tells the program which operation to perform */ int ID; /* to scan the student's ID */ struct student students[1000]; /* sets up the array */ Student *current_student; Student *ptr_to_array; /* points to where the array starts*/ ptr_to_array = &students[0]; /* assumes it is an empty array */ int e; /* to hold the value of the end of the array */ do { scanf("%d", &action); switch(action) { case 0: printf("entered case 0\n"); return 0; break; case 1: printf("Ready to scan student ID\n"); scanf("%d", &ID); current_student = create_student(ID); add_student(ptr_to_array,current_student,e); etc
This generates a compiler warning: "assignment from incompatible pointer type".Code:Student create_student(int n) { Student new_student = (Student) malloc(sizeof(struct student)); new_student->ID=n; new_student->units=NULL; return new_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."Code:void add_student(Student a, Student c, int e) { int i; Student temp; Student previous; a[e] = c; /* put the created student into the end of the array */ for (i = e-1; i=0; i--) { &previous = a[e-1]; if (a[e]<a[e-1]) { &temp = &c; &c = &previous; &previous = &temp; } } e++; }
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.
thanks



LinkBack URL
About LinkBacks



