ccompiler-errorsargumentsfunction-callincompatibletypeerror

Inserting element into array containing a queue of structs


I have an array that contains a pointer to a queue, the queue contains structs, I'm not sure what the problem is here but it's at InsertionDecroissant right before the return.

Compiling problem:

dem.c: In function ‘InsertionDecroissant’:
dem.c:240:36: error: incompatible type for argument 1 of ‘InsertionDecroissant’
  f.t->suiv=InsertionDecroissant(f.t->suiv,d,pts); // if none of those conditions work, recall the same function but with the next element in queue.
                                 ~~~^~~~~~
dem.c:228:32: note: expected ‘File’ {aka ‘struct <anonymous>’} but argument is of type ‘struct maillon *’
 File InsertionDecroissant(File f,DemLogement d,int pts)
                       ~~~~~^

Code:

    typedef struct
    {
        int     handicape;
        int     violenceCouple;
        int     hebergTemp;
        int     pasDeLogement;
        int     logDangereux;
    } Point;
    
    typedef struct
    {
        Point       points;
        char        nomDem[30];
        char        prenomDem[30];
        int         nbrDemandeur;
        int         ressourcesAnnu;
    } DemLogement;
    
            typedef struct maillon {
                DemLogement d;
                int pt;
                struct maillon *suiv;
            } Maillon;
            
            typedef struct {
                Maillon *t;
                Maillon *q;
            } File;
            
            
            void TraitementDem(DemLogement *tdem,int nbDem)
            {
                File tab[6];
                int type,pts;

                for (int i = 0; i < nbDem; ++i)
                {
                    type = TypeLogement(tdem[i].nbrDemandeur); // gets the type of house the applicant needs.
                    pts = atribPoint(tdem[i]); // calculates the total amount of points the applicant can have
                    tab[type] = InsertionDecroissant(tab[type],tdem[i],pts);
                }
            }
            
            File InsertionDecroissant(File f, DemLogement d, int pts)
            {
                if(vide(f)) // if queue is empty
                {
                    f=filenouv(); // Creates new queue
                    return adjq(f,d,pts); // Inserts struct at the beggining of the queue (d) is the struct.
                }
            
                if(pts>=f.t->pt) // if the value passed by the parent func is > or equals the value inside the queue
                    return adjq(f,d,pts);// Inserts struct at the beggining of the queue (d) is the struct.
            
                f.t->suiv=InsertionDecroissant(f.t->suiv,d,pts); // if none of those conditions work, recall the same function but with the next element in queue.
                return f;
            
            }
        File adjq(File f, DemLogement x,int pts) // adds element to queue
        {
            Maillon *m;
            m=(Maillon *)malloc(sizeof(Maillon));
            if(m==NULL)
                {printf("Probleme malloc files.\n");exit(1);}
            m->d=x;
            m->pt=pts;
            m->suiv=NULL;
            if(vide(f))
            {
                f.t=m;
                f.q=m;
                return f;
            }
            f.q->suiv=m;
            f.q=m;
            return f;
        }
File filenouv(void)
{
    File f;
    f.t=NULL;
    f.q=NULL;
    return f;
}

bool vide(File f)
{
    if(f.t==NULL)
        return true;
    return false;
}
int TypeLogement(int nbr)
{
    if(nbr==1 || nbr==2)
        return 0;
    if(nbr==3)
        return 1;
    if(nbr==4)
        return 2;
    if(nbr==5)
        return 3;
    if(nbr==6)
        return 4;
    if(nbr>7)
        return 5;
}

int atribPoint(DemLogement d)
{
    int total;
    total = d.points.handicape * 30 + d.points.violenceCouple * 15 + d.points.hebergTemp * 15 + d.points.pasDeLogement * 10 + d.points.logDangereux  * 8;
    return total;
}

Solution

  • InsertionDecroissant expects a list (File) for the first argument, but receives a node (struct maillon) in the recursive call.

    Get rid of the needless recursion. Instead, use a loop that finds the pointer to modify.

    // Type-safe version of malloc.
    #define MALLOC(T, n) ((T*)malloc(sizeof(T) * n))
    
    // Return 0 on success.
    // Returns -1 and sets `errno` on error.
    int InsertionDecroissant( File *f, DemLogement d, int pt ) {
       // Un pointeur au pointeur que l'on veut modifier.
       Maillon *suivant_ptr = &( f->t );
    
       // On trouve où insérer.
       while ( *suivant_ptr && pt > (*suivant_ptr)->pt )
          suivant_ptr = &( (*suivant_ptr)->suivant );
    
       // On crée un nouveau maillon.
       Maillon *nouveau = MALLOC( Maillon, 1 );
       if ( !nouveau )
          return -1;
    
       nouveau->d       = d;
       nouveau->pt      = pt;
       nouveau->suivant = *suivant_ptr;
    
       // On insère le nouveau maillon.
       *suivant_ptr = nouveau;
    
       // On ajuste la queue si nécessaire.
       if ( !nouveau->suivant )
          f->q = nouveau;
    
       return 0;
    }
    

    The caller would look something like this:

    if ( InsertionDecroissant( &tab[type], d, pt ) == -1 ) {
       perror("Can't insert node");
       exit(1);
    }