分段错误和链接列表的未知问题

所以我正在尝试编写一个机械程序。 程序很长但是这里有导致我问题的函数: recherche_noecreation_noe 。 不需要打扰其余的。

这是法语所以请耐心lst_noe我的想法是这样的:首先在main我向用户询问lst_noenoe lst_noe (这是noe列表)。 使用creation_noe他在向用户询问结构信息时做出了这一点。 最后recherche_noe返回我正在寻找的noe 。 所有信息都存储在struct maillage ,你有其他结构。 谢谢您的帮助。

 #include  #include  #include  /*==============================================*/ /* Déclaration des structures */ /*==============================================*/ struct matrice { char nom[20]; int n,m; double **tab; struct matrice *next; }; struct element { int num; int n1, n2; double k; struct element *next; }; struct noeud { int num; double u; double f; struct noeud *next; }; struct maillage { int nb_noe, nb_elt; struct noeud *lst_noe; struct element *lst_elt; struct matrice *K, *U, *F; }; typedef struct matrice* matrices; typedef struct element* elements; typedef struct noeud* noeuds; typedef struct maillage* maillages; char buffer[100]; /*==============================================*/ /* Recherche */ /*==============================================*/ noeuds recherche_noe(maillages mail,int num){ int i; maillages temp=mail; while(temp->lst_noe!=NULL){ if(temp->lst_noe->num == num) return temp; temp->lst_noe=temp->lst_noe->next; } printf("Le noeud recherche n'existe pas"); return temp; } elements recherche_elt(maillages mail,int num){ int i; maillages temp=mail; while(temp->lst_elt->num != num /*&& temp->lst_elt->next!=NULL*/){ temp->lst_elt=temp->lst_elt->next; } if(temp->lst_elt->num != num /*&& temp->lst_elt->next==NULL*/){ printf("L'element recherche n'existe pas"); }else{ return mail->lst_elt; } } /*==============================================*/ /* creation */ /*==============================================*/ matrices creation_noeud(maillages mail){ int i; for (i=0;inb_noe;i++){ noeuds new = (noeuds)malloc(sizeof(struct noeud)); new->num = i+1; printf("Deplacement du noeud %d: ",i+1); buffer[0]='\0'; getchar(); //reinitialisation de buffer scanf("%[^\n]",buffer); if((int) strlen(buffer)){ //si la taille du buffer différente 0 new->u= (double)atof(buffer); } else{ printf("Donner l'effort %d du noeuds",i+1); scanf("%lf", &new->f); } new->next=mail->lst_noe; mail->lst_noe=new; } } void creation_element(maillages mail) { int i; for (i=0;inb_elt;i++){ elements new= (elements)malloc(sizeof(struct element)); new->num=i+1; printf("Donner le noeud 1 de l'element %d: ",i+1); scanf("%d", &new->n1); printf("Donner le noeud 2 de l'element %d: ",i+1); scanf("%d", &new->n2); printf("Donner la raideur de l'element %d: ",i+1); scanf("%lf",&new->k); new->next= mail->lst_elt; mail->lst_elt=new; } } matrices creation_mat(int n,int m, char *nom){ int i,j; matrices new=(matrices)malloc(sizeof(struct matrice)); strcpy(new->nom,nom); new->n = n; new->m = m; new->tab = (double**)malloc((n)*sizeof(double*)); for (i=0; itab[i] = (double*)malloc((n)*sizeof(double)); for (i=0;i<n;i++) /* mise a zero des composantes */ for (j=0;jtab[i][j] =0; return new; } /*==============================================*/ /* Assemblage */ /*==============================================*/ void assemblage(maillages mail){ int a,b,i,j,k; mail->K = creation_mat(mail->nb_noe, mail->nb_noe,"K"); mail->U = creation_mat(mail->nb_noe, 1,"U"); for (j=0; jnb_noe; j++){ //Initialisation de K for(k=0; knb_noe; k++){ mail->K->tab[j][k]=0; } } printf("%d",recherche_elt(mail,i+1)->n1); for (i=0; inb_elt; i++){ // Assemblage matrice K a = recherche_elt(mail,i+1)->n1-1; b = recherche_elt(mail,i+1)->n2-1; mail->K->tab[a][a] +=recherche_elt(mail,i+1)->k; mail->K->tab[a][b] -=recherche_elt(mail,i+1)->k; mail->K->tab[b][a] -=recherche_elt(mail,i+1)->k; mail->K->tab[b][b] +=recherche_elt(mail,i+1)->k; } for (i=0; inb_noe; i++){ // Assemblage matrice U mail->U->tab[i][0] = recherche_noe(mail,i+1)->u; } } /*==============================================*/ /* Produit */ /*==============================================*/ matrices produit(matrices mat1,matrices mat2,char *nom){ int i,j,k; matrices prod; if(mat1->m!=mat2->n){ printf("Erreur, les matrices ne sont pas compatibles\n\n"); }else{ prod=malloc(sizeof(struct matrice)); strcpy(prod->nom,nom); prod->next=NULL; prod->n=mat1->n; prod->m=mat2->m; prod->tab= (double **)malloc(prod->n * sizeof(double *)); for (i=0; in; i++) prod->tab[i] = (double *)malloc(prod->m * sizeof(double)); for (i=0;in;i++){ for (j=0;jm;j++){ prod->tab[i][j]=0; for (k=0;km;k++){ prod->tab[i][j]+=mat1->tab[i][k] * mat2->tab[k][j]; } } } return prod; } } /*==============================================*/ /* Affichage */ /*==============================================*/ void affiche_mat(matrices mats){ int i,j; printf("Matrice %s de dimensions %d*%d:\n",mats->nom,mats->n,mats->m); for (i=0;in;i++){ for (j=0;jm;j++){ printf("%s[%d][%d]: %lf\n",mats->nom,i,j,mats->tab[i][j]); } } printf("\n"); } int main(){ int i; elements lst_elt; noeuds lst_noe; maillages mail=malloc(sizeof(struct maillage)); mail->lst_noe=NULL; mail->lst_elt=NULL; printf("Donner le nombre de noeuds voulu: "); scanf("%d",&mail->nb_noe); printf("Donner le nombre d'elements voulu: "); scanf("%d",&mail->nb_elt); creation_noeud(mail); creation_element(mail); printf("%d",recherche_elt(mail,2+1)->n1+45); assemblage (mail); produit(mail->K,mail->U,"F"); /*affiche_mat(mail->K); printf("\n"); affiche_mat(mail->U); printf("\n"); affiche_mat(mail->F); printf("\n");*/ } 

在遍历链表时,您正在改变链表的内容,而不是提升临时表。


 elements recherche_elt(maillages mail,int num){ elements temp; for (temp = mail->lst_elt; temp; temp = temp->next ) { if (temp->num == num) return temp; } printf("L'element recherche n'existe pas\n"); return NULL; } 

并且,在删除typedef后,它变为:


 struct element *recherche_elt(struct maillage *mail,int num){ struct element *this; for (this = mail->lst_elt; this; this = this->next ) { if (this->num == num) return this; } printf("L'element recherche n'existe pas\n"); return NULL; } 

[同样的错误在代码中的其他地方再现]