奇怪的值添加到列表中

0x6*_*C74 1 c list

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


struct a
{
    a * next;
    double v;
};


void add(struct a* list,double d)
{
    if(!list->next) goto exception; //I know that a lot of programmers have a low opinion about "goto"

    list=list->next;

    list->v=d;

    return;

exception:
    printf("Cannot add a new element to the list\n");
}

int main()
{
    struct a l;
    double j;
    int i;

    for(j=1.0; j<10.0; j+=1.0)
    {   
        l.next= (a*)malloc(sizeof(a));
        add(&l,j);
        printf("%lf ",l.v);
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

这个程序编译,但输出中有一个混乱:

-92559631349317831000000000000000000000000000000000000000000000.000000 -92559631 349317831000000000000000000000000000000000000000000000.000000 -92559631349317831 000000000000000000000000000000000000000000000.000000 -92559631349317831000000000 000000000000000000000000000000000000.000000 -92559631349317831000000000000000000 000000000000000000000000000.000000 -92559631349317831000000000000000000000000000 000000000000000000.000000 -92559631349317831000000000000000000000000000000000000 000000000.000000 -92559631349317831000000000000000000000000000000000000000000000 0.000000 -92559631349317831000000000000000000000000000000000000000000000.000000

期望的是:

1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0

错误在哪里以及如何解决?

hmj*_*mjd 7

问题是,l.vmain()从未作为赋值add()价值分配l.next.的分配listlist->next如此不给调用者可见lmain()永远是同一个实例struct a.意思prinf()是打印相同的单位化double.

其他要点: