[英]Inserting element into array containing a queue of structs
我有一個包含指向隊列的指針的數組,隊列包含結構,我不確定這里的問題是什么,但它在返回之前位於InsertionDecroissant
。
編譯問題:
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)
~~~~~^
代碼:
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 ammount 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;
}
錯誤:“InsertionDecroissant”的參數 1 的類型不兼容
此錯誤消息意味着您使用的第一個參數類型不正確,而不是 function 預期的第一個參數。
例如,在此語句中存在 function 調用
f.t->suiv=InsertionDecroissant(f.t->suiv,d,pts);
第一個參數表達式ft->suiv
沒有 function InsertionDecroissant
的第一個參數的預期類型。 相反,如果File
類型的表達式由於此聲明而具有struct maillon *
類型
typedef struct maillon{
DemLogement d;
int pt;
struct maillon *suiv;
}Maillon;
嘗試從編譯器錯誤中發布更易於理解的 output。 此外,請查看 function 定義中的不匹配類型,並注意 Vlad 的評論。
不相關,但是,為什么要按值傳遞所有 arguments ?
InsertionDecroissant
需要一個列表( File
)作為第一個參數,但在遞歸調用中接收一個節點( struct maillon
)。
擺脫不必要的遞歸。 相反,使用循環來查找要修改的指針。
// 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;
}
調用者看起來像這樣:
if ( InsertionDecroissant( &tab[type], d, pt ) == -1 ) {
perror("Can't insert node");
exit(1);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.