Thread: Inserting new member into array of structs

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    7

    Unhappy Inserting new member into array of structs

    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:
    Code:
    typedef struct student *Student;
    struct student
    {
    	int ID;
    	node_ptr units;
    };
    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:
    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
    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:
    Student create_student(int n)
    {
      Student new_student = (Student) malloc(sizeof(struct student));
      new_student->ID=n;
      new_student->units=NULL;
      return new_student;
    }
    This generates a compiler warning: "assignment from incompatible pointer type".

    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.

    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++;
    }
    This gives the compiler warning: "passing arg 2 of 'add_student' from incompatible pointer type."

    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
    Last edited by cathym; 04-17-2005 at 05:46 AM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You do know that your array is an array of actual structures and not pointers to structures, right? As such, you don't need to allocate anything. All you need to do is fill in their values. If that's not what you want, then you need to change it so it's an array of Student. (Pet peeve: I hate hidden pointer typedefs.)

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    7
    Thanks for the quick response.
    Isn't Student the actual structure though? So the line

    Code:
     
    struct student student[1000]
    creates an array of 1000 structs of the type Student, which is called students.
    Is that right? Or am I way off track.. The assignment spec says the array 'struct student students[1000];' stores the data about the students, in students[0] to students[n_students-1]. n_students is a separet variable that holds the number of students in the array. Am I right in saying that the lines:
    Code:
    struct student students[1000];
    and
    Code:
    Student students[1000];
    mean the same thing?

    How should I write this so that it meets what is in the assignment spec?
    And, sorry about the typedef thing, I am just copyng what is taught in the lectures.....

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    No. They're two entirely different things. Look at what you have:
    Code:
    typedef struct student *Student;
    struct student
    {
    	int ID;
    	node_ptr units;
    };
    This defines Student as "a pointer to a struct student".

    Therefore, if we make a couple of arrays:
    Code:
    struct student foo[5];
    Student bar[5];
    The first one is five actual instances of the structure 'struct student'.
    The second one is five Student instances. Which, because of the way you've defined it, means that you have an array of five "pointers to a struct student".

    Think of it as this:
    Code:
    typedef int * ptrtoint;
    
    int array[5];
    ptrtoint differentarray[5];
    Here, 'array' is five actual integers, and 'differentarray' is five pointers to integers.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Apr 2005
    Posts
    7
    Oh I think I see!
    If it was defined as:
    Code:
    typedef struct student Student;
    struct student
    {
    	int ID;
    	node_ptr units;
    };
    then
    Code:
    struct student students[1000]
    would be the same as
    Code:
    Student students[1000]
    Ok. If defined as above ie an array of structs, not pointers, then to add a new student into the array, would I simply scanf the ID and put it into students[0] (presuming the array was empty) or students[e] where e is the value of the next unfilled spot in the array. What would that code look like?

    In main:
    Code:
    scanf("%d", &ID);
    students[e]=ID;
    I don't understand how to put the ID that is scanned into the ID part of the struct. Sorry, it's late and I have been looking at this for a long time.............

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It would be:
    Code:
    struct foo
    {
        int x;
    };
    
    struct foo myfooinstance;
    
    myfooinstance.x = somevalue;
    Where . is the operator used for accessing members of a structure (you use the arrow operator -> if the item to the left was a pointer to a structure and not an actual structure instance), and where x is the actual member you wish to access.
    Code:
    students[e].ID = ID;
    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Apr 2005
    Posts
    7
    Ahh
    Many many thanks, and now I shall go to bed without pointer nightmares and will re-write the code in the morning.
    Thanks again

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating array of structs
    By knirirr in forum C++ Programming
    Replies: 12
    Last Post: 06-18-2008, 08:30 AM
  2. pointer to array of structs
    By Luken8r in forum C Programming
    Replies: 2
    Last Post: 01-08-2008, 02:05 PM
  3. Sorting an Array of Structs not working
    By ashcan1979 in forum C++ Programming
    Replies: 9
    Last Post: 09-28-2006, 03:07 PM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM