鱼C论坛

 找回密码
 立即注册
查看: 3249|回复: 7

关于链表传入一个指针

[复制链接]
发表于 2022-4-20 22:36:22 | 显示全部楼层 |阅读模式
10鱼币
请问下面这两种传法,传的都是地址,差别具体在哪呢

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

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

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

  9.     /* move the head to point to the new node */
  10.     (*head_ref) = new_node;
  11. }
复制代码

  1. Node* push(Node* head,int x){
  2.     list number = (list)malloc(sizeof(struct LISTnode));
  3.     number->x = x;
  4.     number->next = head;
  5.     head = number;
  6.     return head;
  7. }
复制代码

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2022-4-20 22:37:11 | 显示全部楼层
作为插入一个结点这么一个操作,2种方式好像都能实现欸
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2022-4-20 22:49:02 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-4-21 11:26:24 From FishC Mobile | 显示全部楼层
一个有返回(返回指针),一个无返回
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2022-4-23 20:50:32 | 显示全部楼层
傻眼貓咪 发表于 2022-4-21 11:26
一个有返回(返回指针),一个无返回

传入方式呢,一个传的是指针,一个是指针的指针,后者有啥好处吗
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-4-23 21:28:54 | 显示全部楼层
桥驿听雨落 发表于 2022-4-23 20:50
传入方式呢,一个传的是指针,一个是指针的指针,后者有啥好处吗

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

  2. int funcA(int a) {
  3.     return a + 3;
  4. }

  5. void funcB(int *a) {
  6.     *a += 3;
  7. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2022-4-25 18:55:14 | 显示全部楼层
傻眼貓咪 发表于 2022-4-23 21:28
给你点简单的,你看得出以下两个函数的分别吗?

就一个是传回一个值,另一个是直接对a进行改变
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-4-25 19:47:58 | 显示全部楼层
桥驿听雨落 发表于 2022-4-25 18:55
就一个是传回一个值,另一个是直接对a进行改变

就如你的问题一样,一个是返回头指针,一个是对头指针进行改变
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-1 12:52

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表