c - 即使在整整两天后也完全看不出是什么导致了段错误

标签 c linked-list segmentation-fault

问题出在 main 中的 printf 上。我不是程序员,这是自学的,所以不要伤害我。函数creation_noe创建一个noes的链接列表。如果我们想要其中的 3 个,那么链接将从编号为 3 的那个开始,但是 main 中的 printf 给了我 1 3 3。我迷路了。 感谢您的帮助。

PS:程序比较长,这只是需要的部分。如果您有疑问,我会在这里回答。我只是不想让你把时间浪费在其他无关紧要的事情上。谢谢

P.P.S:这些条款是法语

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
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 element* elements;
    typedef struct noeud* noeuds;
    typedef struct maillage* maillages;
    
    
    /*==============================================*/
    /*               Recherche       */
    /*==============================================*/
    
    noeuds recherche_noe(maillages mail,int num){
        int i;
        maillages temp=mail;
        printf("%d %d ",num,mail->lst_noe->num);
    
        while((temp->lst_noe!=NULL) && (temp->lst_noe->num!=num)){
            temp->lst_noe=temp->lst_noe->next;
        }
        if(temp->lst_noe->num==num){
            return temp->lst_noe;
        }
        return NULL;
    }
    
    /*==============================================*/
    /*                   creation                   */
    /*==============================================*/
    
    
    
    void creation_noeud(maillages mail){
        int i;
        noeuds new = (noeuds)malloc(sizeof(struct noeud));
        mail->lst_noe=NULL;
    
        for (i=0;i<mail->nb_noe;i++){
            new->num = i+1;
            printf("Deplacement du noeud %d: ",new->num);
            scanf("%lf", &new->u);
    
    
    
    
            new->next=mail->lst_noe;
            mail->lst_noe=new;
        }
    }
    
    int main(){
        int i;
        maillages mail=malloc(sizeof(struct maillage));
    
        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("aaa %d %d %d aaa \n",1,mail->lst_noe->num,mail->lst_noe->next->num,mail->lst_noe->next->next->num);
    }

最佳答案

mail->lst_noe=NULL;

...

new->next=mail->lst_noe;
mail->lst_noe=new;


and then

mail->lst_noe->next->next->num

这肯定会产生段错误,因为mail->lst_noe->next只不过是mail->lst_noe,它本身就是NULL。取消引用空指针会产生 SEGFAULT。

关于c - 即使在整整两天后也完全看不出是什么导致了段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50445601/

相关文章:

c - 如何检查数据是否仍在内存中

c - 在 C 链表中,为什么节点也是指针?

java - 理解链表

matlab - 检测到分段违规 - matlab

c - 如何让 XCode 6、LLVM 编译器检查 c 函数参数

c - 如何使用 C 中读取的函数显示文件内容?

java - 如何在 Java 中的同一个列表中添加多个对象类型

c++ - 在 C++ 中使用数组获取段错误

c - 在 sprintf 中使用节点元素

c - 如何从文件读入并从缓冲区输出到另一个数组