[英]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.