桥驿听雨落 发表于 2022-4-20 22:36:22

关于链表传入一个指针

请问下面这两种传法,传的都是地址,差别具体在哪呢

void push(struct Node** head_ref, int new_data)
{
    /* allocate node */
    struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));

    /* put in the data*/
    new_node->data = new_data;

    /* link the old list off the new node */
    new_node->next = (*head_ref);

    /* move the head to point to the new node */
    (*head_ref) = new_node;
}

Node* push(Node* head,int x){
    list number = (list)malloc(sizeof(struct LISTnode));
    number->x = x;
    number->next = head;
    head = number;
    return head;
}

桥驿听雨落 发表于 2022-4-20 22:37:11

作为插入一个结点这么一个操作,2种方式好像都能实现欸{:10_249:}

桥驿听雨落 发表于 2022-4-20 22:49:02

{:10_266:}

傻眼貓咪 发表于 2022-4-21 11:26:24

一个有返回(返回指针),一个无返回

桥驿听雨落 发表于 2022-4-23 20:50:32

傻眼貓咪 发表于 2022-4-21 11:26
一个有返回(返回指针),一个无返回

传入方式呢,一个传的是指针,一个是指针的指针,后者有啥好处吗

傻眼貓咪 发表于 2022-4-23 21:28:54

桥驿听雨落 发表于 2022-4-23 20:50
传入方式呢,一个传的是指针,一个是指针的指针,后者有啥好处吗

给你点简单的,你看得出以下两个函数的分别吗?#include <stdio.h>

int funcA(int a) {
    return a + 3;
}

void funcB(int *a) {
    *a += 3;
}

桥驿听雨落 发表于 2022-4-25 18:55:14

傻眼貓咪 发表于 2022-4-23 21:28
给你点简单的,你看得出以下两个函数的分别吗?

就一个是传回一个值,另一个是直接对a进行改变

傻眼貓咪 发表于 2022-4-25 19:47:58

桥驿听雨落 发表于 2022-4-25 18:55
就一个是传回一个值,另一个是直接对a进行改变

就如你的问题一样,一个是返回头指针,一个是对头指针进行改变 {:10_269:}
页: [1]
查看完整版本: 关于链表传入一个指针