【问题标题】:C - Linked list segment faultC - 链表段错误
【发布时间】:2017-03-30 05:13:02
【问题描述】:

将元素放入我的链表后,当我想访问它们时,我遇到了分段错误。我尝试从头部插入(头部是 tete),在读取元素时我只在那个函数中没有问题

这是导致分段错误错误的行:

if((p->ID.num)>(p2->ID.num))




#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <conio.h>

        typedef struct identifiant//identifiant
        {
            char section[50];
            int num;
        }identifiant;

         typedef struct Date //DATE
        {
            int jj;
            int mm;
            int an;
        }Date;

           typedef struct notes
        {
            float note;
            struct notes* nnext;
        }notes;

          typedef struct LTE
        {
         identifiant ID;
         char Nom[25];
         char Prenom[25];
         Date Date_naissance;
         notes* Tnotes;
         float Moy;
         struct LTE* next;
        }LTE;

         typedef struct coefs
        {
            int coef;
            struct coefs* next;
        }coefs;
           coefs* COEF;
       LTE* tete;


    int main()
    { int NE,NN;

           LTE* p;
           LTE* n;
           LTE* m;



         coefs* q;
       int i,x;
       x=0;

           NE = Saisie_NE();
         NN = Saisie_NN();

         {
         tete=(LTE*)malloc(sizeof(LTE));
         tete->next=0 ;
         Saisie_E(1,tete,NN);
         for(i=2;i<=NE;i++)
          {
           LTE* tmp=(LTE*)malloc(sizeof(LTE));
           Saisie_E(i,tmp,NN);
           tmp->next=tete;
           tete=tmp;
           }
         }....
         //remplir tabeleau des coefs
         {
          COEF=(coefs*)malloc(sizeof(coefs));
          COEF->next=0 ;
          q=COEF;
         for(i=0;i<NN;i++){
           Saisie_coef(i+1,q,NN,&x);
           coefs* tmp=(coefs*)malloc(sizeof(coefs));
           q->next=tmp;
           q=q->next;
         }
         q->next=0;
         }
         //everything works fine until the this function↓↓↓
         {
             p=tete;


        Trier(p,NE);
     }

//here is the functuion ty guys sorry for bad presentation


    void Trier(LTE* p,int NE)
    {
       int tr,i;
       LTE* q;
       LTE* p1;
       LTE* p2;
       p1=p;
    i=0;
    while(tr!=1)
    {   tr=1;
        p=p1;
        for(i=0;i<NE;i++)
        {   p2=p->next;
    //here is the segment fault error

            if((p->ID.num)>(p2->ID.num)) 
     {q=p->next->next;
                p->next->next=p;
                p->next=q;
                tr=0;
                }
     p=p->next;
        }
    }

【问题讨论】:

  • Saisie_NESaisie_NNSaisie_E 的代码吗?你的括号也不匹配。您应该按照here 的说明制作一个最小完整的可验证示例

标签: c linked-list segmentation-fault


【解决方案1】:

问题在于下面的循环。在循环迭代期间,当i == (NE-1) 时,p 将指向最后一个节点,而p-&gt;next 将为分配给p2 的NULL。因此,访问p2-&gt;ID.num 会导致分段错误。

您可以添加对p2!=NULL 的检查或修改循环逻辑以防止这种情况发生。

 for(i=0;i<NE;i++)
    {   p2=p->next; /* ==> When p becomes the last node, p2 will become NULL */
//here is the segment fault error

        if((p->ID.num)>(p2->ID.num)) 
 {q=p->next->next;
            p->next->next=p;
            p->next=q;
            tr=0;
            }
 p=p->next;
    }

【讨论】:

  • 如果它有助于解决您的问题,请点赞并接受答案。谢谢。 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-10
  • 2015-12-31
  • 1970-01-01
  • 1970-01-01
  • 2011-03-14
相关资源
最近更新 更多