小编Lun*_*una的帖子

将链表头传递给函数作为C中的地址

我有一个关于通过函数传递C中链表头部的问题.所以代码是这样的:

#include <stdio.h>
//Defining a structure of the node
struct node { 
    int data;
    struct node* next;
    };

void insert (struct node* rec, int x) {
    struct node* temp = (struct node*)malloc(sizeof(struct node));
    temp->data = x;
    temp->next = NULL;
    rec = temp; // head and rec is now pointing to the same node
}

void print(struct node* rec){
    printf("%d", rec->data); //error occurs here
    puts("");
}

main(){
    struct node *head = NULL; //head is currently pointing to NULL
    insert (head, 5); …
Run Code Online (Sandbox Code Playgroud)

c pointers linked-list pass-by-reference

2
推荐指数
1
解决办法
1万
查看次数

标签 统计

c ×1

linked-list ×1

pass-by-reference ×1

pointers ×1