Thread: incompatiable data types error

  1. #1
    Registered User
    Join Date
    Oct 2013
    Posts
    9

    incompatiable data types error

    I almost have it. This program will take the struct person student[] which contains 5 students it will first print the students and then it will sort them by age.

    I have written the print_person_info and I have almost finished sort_by_age.

    But I get the error: incompatible types when assigning struct person from int and incompatible types when assigning int from struct person.

    I have a feeling I need to use pointer notation. ex. (&, *) in front of temp. But I am not sure how to use the notation..thank you for your help.

    Code:
    #include <stdio.h>
    struct person
    {
       char first_name[20];
       char last_name[20];
       int age;
    };
    void print_person_info(struct person clone);
    void sort_by_age(int n, struct person a[]);
    
    int main(void)
    {
       int i, n=5;
      struct person student[5] =
      {
       { "Bob",   "Smith",    21},
       { "Jimmy", "John",     18},
       { "Amy",   "Goldberg", 20},
       { "Dan",   "Marlo",    17},
       { "Sally", "Sorrow",   16}
      };
       for(i=0; i<n; i++)
        print_person_info(student[i]);
        sort_by_age(n, student);
      
    printf("------AFTER SORTING-------\n");
     for(i=0; i<n; i++)
      print_person_info(student[i]);
    
                    return 0;
    }
    void print_person_info(struct person clone)
    {
     int i=0;
    
     for(i=0; i<5;i++)
     {
       printf("First Name: %s Last Name: %s Age: %d \n", clone.first_name, clone.last_name, clone.age);
     }
    }
    
    void sort_by_age(int n, struct person a[])
    {
         int i=0, j, temp;
      
         for(i=0; j<n-1;i++)
         {
          for(j=i+1;j<n;j++)
          { 
           if(student[i].age > student[j].age)
             {
                  temp =student[i].age;
                  student[i].age=student[j].age;
                  student[j].age=temp;
              }
           }
        }
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    They tell you what the incompatible types are: int and struct person. There are no * here, so & can't help.

    You need to make temp a person, not an int. (This exact statement was posted in your other thread, though not by me, so all credit to that poster. You should maybe read the replies that you get before posting again.)

  3. #3
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    Quote Originally Posted by SupermanC View Post
    Code:
    ...
    void sort_by_age(int n, struct person a[])
    {
    ...
           if(student[i].age > student[j].age)
             {
                  temp =student[i].age;
                  student[i].age=student[j].age;
                  student[j].age=temp;
              }
    ...
    }
    Your parameter name is 'a' but you use the name 'student' in the function, change 'a' to 'student'.

    Also, please use consistent indentation, set a standard tab with of 4 characters (or something of that nature, 4 tends to be widely used) and keep it consistent throughout all indentation levels.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by nonpuz View Post
    Your parameter name is 'a' but you use the name 'student' in the function, change 'a' to 'student'.

    Also, please use consistent indentation, set a standard tab with of 4 characters (or something of that nature, 4 tends to be widely used) and keep it consistent throughout all indentation levels.
    Your sort function - needs some work:

    Code:
    void sort_by_age(int n, struct person a[])
    {
         int i=0, j
         struct person temp;   //temp is now a struct
                                      //so it can hold a struct! ;) 
       
         for(i=0; i < n-1;i++)  //not j<n, but i < n
    
         {
          for(j=i+1;j<n;j++)
          {
    
           if(student[i].age > student[j].age)
             {
               //if we are swapping structs, we have to swap the 
               //entire struct - not just the age.
    
                  temp = student[i];
                  student[i] = student[j];
                  student[j] = temp;
              }
           }
        }
    }
    Swapping like this works because:

    1) You put the prototype of the struct, up above main(), outside any function (it's global). That makes it easy to make a temp struct of type student.

    2) We can exchange an entire struct, all at once, as long as every struct member is static (fixed in size), not dynamic (variable).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Error: Incompatiable types in assignment
    By Roadrunner2015 in forum C Programming
    Replies: 2
    Last Post: 10-11-2013, 08:08 AM
  2. Data Types
    By loftinsulation in forum C++ Programming
    Replies: 7
    Last Post: 03-07-2008, 10:15 AM
  3. data types types...
    By gftmc in forum C++ Programming
    Replies: 3
    Last Post: 09-11-2006, 11:30 AM
  4. data types
    By Micko in forum Windows Programming
    Replies: 2
    Last Post: 05-18-2004, 06:23 AM
  5. data types
    By wazilian in forum C Programming
    Replies: 1
    Last Post: 12-07-2001, 01:52 PM

Tags for this Thread