使用指针将内存分配给结构

Agn*_*oc -1 c struct list

我有以下链表:

struct node {
    int d;
    struct node *next;   
};

int main()
{
    struct node *l = 0;
    struct node *k = l;
    k = malloc(sizeof(struct node));
    /* l->d = 8; */
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

为什么注释代码使用错误?我不明白为什么内存没有分配给k指向同一个节点的节点l,我使用k-pointer为它分配内存.

Eug*_*Sh. 5

让我们分开吧.看看评论

struct node{
    int d;
    struct node * next;   
};

int main(){
    struct node * l = 0;     // Now l = 0 (or NULL)
    struct node * k = l;     // Now k=l=0
    k = malloc(sizeof(struct node));   // Now k=<some address allocated>
    /*
    l->d = 8;                          // But l is still 0
    */
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

因此注释的代码试图取消引用NULL指针.