指针指向链表中的错误元素 (C)

Pointer points on wrong element in linked list (C)

我想在我的程序中实现一个喜欢的列表: 前两个元素已正确插入,但接下来的元素开始指向它们自己:(如您在我的输出中所见)

0
(nil)
1
0x7fffb3899340
(nil)
2
0x7fffb3899350
0x7fffb3899350
3
0x7fffb3899350
0x7fffb3899350

这是我的方法,它应该 return 指向第一个元素的指针:

Filediff* compare(File f1, File f2){
    int line = 0;
    Filediff *first = &(Filediff){line, 0, NULL}; //First elem
    char c1,
         c2;

    printf("w1\n");
    while ((c1 = fgetc(f1.content)) != EOF && (c2 = fgetc(f2.content)) != EOF)
    {
        if(c1 == '\n' || c2 == '\n'){
            line++;
            printf("%p\n", first->next);
                if(line >= 2)
                    printf("%p\n", first->next->next);
            first = &(Filediff){line, 0, first}; //insert last elem in new record.
        }else if(c1 != c2)
            first->charCount++;
        printf("%c %c\n", c1, c2);
    }

    fclose(f1.content);
    fclose(f2.content);

    return first;
}

我列表中的一条记录如下所示:

typedef struct Filediff
{
    int line;
    int charCount;
    struct Filediff *next;
} Filediff;

感谢你帮我解决这个问题!

这是在我的案例中实现链表的正确方法。

Filediff* compare(File f1, File f2){
int line = 0;
Filediff *first;
first = (Filediff*)malloc(sizeof *first);
first->line = line;
first->charCount = 0;
first->next = NULL;
char c1,
     c2;

while ((c1 = fgetc(f1.content)) != EOF && (c2 = fgetc(f2.content)) != EOF)
{
    if(c1 == '\n' || c2 == '\n'){
        printf("%d\n", line);
        line++;
        printf("%p\n", first->next);
            if(line >= 2)
                printf("%p\n", first->next->next);
        Filediff *buf = (Filediff*)malloc(sizeof *first);
        buf->line = line;
        buf->charCount = 0;
        buf->next = first;
        first = buf;
        //first = &(Filediff){line, 0, first};
    }else if(c1 != c2)
        first->charCount++;
}

    fclose(f1.content);
    fclose(f2.content);

    return first;
}