前言(Introduction):

最近在学习链表的过程中,我写了这样一段函数:

Recently in the process of learning the linked-list, I wrote a function like this:

/* Add a new item to the list */
int addItem(struct film *head, struct film *prev, char *input){/* Request memory to store the input and create a pointer to it */struct film *current = (struct film *) malloc(sizeof(struct film));  /* Judge whether the first node exists */if (head==NULL){head = current;}else{prev->next = current;}/* Edit the current node */current->next = NULL;strcpy(current->title, input);puts("[INFO] Please input the rating<0-10> here.");scanf("%d",&current->rating);while(getchar()!='\n')continue;puts("[INFO] Adding an item successfully.");/* set the value of pointer for previous node to the value of current pointer */prev = current;return 0;
}

很明显,我的目的是为了在链表中添加一个新的节点,并将输入的数据存放进去。

Obviously, I want to add a new node to the linked-list, and save the input to it.

运行整个程序时,我调用该函数能正常完成输入,但是在之后我对链表的所有数据进行遍历输出时,我发现其实数据并没有被存入链表里。

Then I ran the whole program, there was nothing wrong with this function. However, when I had a traversal of this list and output all data, I found that nothing is stored in the list.

问题(The Problem):

那么,问题出在哪里呢?

So, what's the problem?

通过调试,我确认了该函数是正常运行的,所以问题在于函数中的操作并不能正确的完成我想要完成的事情。

After debugging, I made sure that the function ran well, so the problem is that my code is unable to do want I want to do correctly.

随后我在该函数被调用结束后,加上了打印当前head的值和prev的值的函数。

Then I add the function to print the value of "head" and "prev" after the function is executed.

两者的结果都是NULL(NULL是我初始化这两个指针变量时所赋的值)。

The results are both NULL, which is what I set to the two pointers when I initial them.

原因(Cause):

我很迷惑,为什么会这样。重新读了几遍代码,都没有发现问题。那就开始单步调试吧。

I am confused, how can that be. Reading the code again and again, I can't find the problem. So I start to debug step by step.

当我看到一切正常运行到函数结束后,"head"的值突然变回NULL时,我恍然大悟。

When I saw the value of "head" changed back to NULL at the moment that all the things ran well and the function is done, the problem suddenly dawned on me.

我将结构的指针传入函数以便改变结构内部的成员变量的值,但是忘了结构指针本身作为被传入的变量,在函数内部是无法改变其值的。

(原因是, C语言函数中传入的变量仅是与被调用时传入的参数的值相同的一份拷贝,与原变量处于两个不同的存储空间)

In this function, I used the pointer to a struct to change the value of the variable in the struct, but I forgot that the value of the point can't be changed as a variable passed in.

(The reason is that the variable passed in the function is just a copy of the original variable, the memories to store them are different in the C language)

解决方案(Solution):

明确问题之后,修改方案就很容易得出了。

As the problem is clear, it's easy to come up with the solution.

现在我既想改变传入的指针的值,又想能够改变指针指向的成员变量的值,怎么办呢?

Now I want to change the value of the pointer passed in, also want to change the value of the variable in the struct which is pointed by the pointer, how to do that?

二级指针!

The secondary pointer!

所以我修改了代码如下:

So I changed the code as below:

/* Add a new item to the list */
int addItem(struct film **head, struct film **prev, char *input){/* Request memory to store the input and create a pointer to it */struct film *current = (struct film *) malloc(sizeof(struct film));  /* Judge whether the first node exists */if (*head==NULL){*head = current;}else{(*prev)->next = current;}/* Edit the current node */current->next = NULL;strcpy(current->title, input);puts("[INFO] Please input the rating<0-10> here.");scanf("%d",&current->rating);while(getchar()!='\n')continue;puts("[INFO] Adding an item successfully.");/* set the value of pointer for previous node to the value of current pointer */*prev = current;return 0;
}

同时将调用该函数时传入的参数改为了指向两个结构的指针的指针。

At the same time, I changed the two parameters to the secondary pointers to the two structs.

运行程序,一切正常。

Run the program, all the things are well.

总结(Conclusion):

回过头来看,这是一个非常简单的问题,但当我在编写和调试这段代码时,始终无法发现问题所在,应了“当局者迷”这句话。当检查代码时,我一直纠结于通过结构指针改变结构体成员变量这一操作,却忽略了结构指针作为参数,本身在传入函数时只是传入了一份与其值相同的拷贝这一问题。所以,当你无法发现你的代码出现了什么问题时,不妨回想一下一些基础概念,也许一切便会豁然开朗。

Looking back on this process, it's a very easy problem, but I can't find that quickly when I am writing and debugging on this code, proving that those closely involved can't see clearly. When checking the code, I focused on the operation that change a variable in the struct through the pointer to the struct, ignoring that the pointer can't be changed as a parameter passed in the function. So, when you can't find what is wrong with your code, why not think back some basic concepts, maybe suddenly you will see the light.

C语言中关于向函数中传入结构指针的易错点及解决方案相关推荐

  1. self计算机语言,python中self在函数中如何使用

    python中self在函数中如何使用 发布时间:2020-12-14 09:12:49 来源:亿速云 阅读:108 作者:小新 这篇文章将为大家详细讲解有关python中self在函数中如何使用,小 ...

  2. validate中remote返回函数中+号导致submitHandler无法执行

    validate中remote返回函数中+号导致submitHandler无法执行 这是2017年以来我遇到的最无语的bug,现在暂时还没想到原因,但是这个错误真的很无语. 这是我的validate中 ...

  3. C语言一级指针(char *)易错模型分析

    C语言一级指针char *易错模型分析 char *(字符串)做函数参数出错模型分析 越界 不断修改指针变量的值 你向外面传递什么 重复的错误何时休 char *(字符串)做函数参数出错模型分析 建立 ...

  4. 【C 语言】C 项目开发代码规范 ( 形参合法性判断 | 函数返回值局部变量 | 函数中不用全局变量 | 函数中使用局部变量接收形参 | 函数返回值 | 形参作返回值 | 形参返回值处理 )

    文章目录 一.C 项目开发代码规范 一.C 项目开发代码规范 上一篇博客 [C 语言]字符串模型 ( 键值对模型 ) 中 , 完成了字符串的 键值对 查找功能 , 代码不太规范 ; C 项目开发代码规 ...

  5. python中如何定义函数的传入参数是option的_python – 当使用@ click.option将命令行参数传递给函数时,如何返回值?...

    我试图使用 click python包将命令行参数传递给函数.官方文档中的示例如解释的那样工作.但是文档中没有提到如何返回值.文档中没有任何函数返回值,因此我不明白如何执行此操作. 以文档为例: im ...

  6. c++中把一个函数中的语句复制到另一个语句中报错_从底层看前端(十一)—— JavaScript语法:脚本,模块和函数体。...

    这篇文章我们继续聊JavaScript语法. 在讲解具体的语法结构之前,先看看语法的一些基本规则. 脚本和模块 首先,JavaScript有两种源文件,一种叫脚本(script),一种叫做模块(mod ...

  7. matlab的syms无法在函数中使用_Python函数中使用@

    Python函数中使用@ 稍提一下的基础 fun 和fun()的区别 以一段代码为例: def fun():print('fun')return Nonea = fun() #fun函数并将返回值给a ...

  8. c语言向自定数组_C语言怎么向自定义函数中传入一个数组,处理完再返回新的数组?...

    展开全部 看你的代码. 你问题并不在数组传入62616964757a686964616fe4b893e5b19e31333431346362.你函数mymd5接收password数组,mymd5的pa ...

  9. 在qt中用c语言数组,在QT函数中返回一个数组/把一个数组传参给函数

    1.把数组传参给函数 可以定义一个QVector的一个数组 QVector num(10); for(int  i =0;i<10;i++) num [i] = i*i; fun(num); / ...

最新文章

  1. 链表问题12——将单链表的每K个节点之间逆序
  2. python format的功能_python format是什么
  3. iOS开发里的Bundle是个啥玩意?!
  4. linux shell 脚本 2,理解Linux Shell和基本的Shell脚本(2)
  5. Asp.Net Core EndPoint 终结点路由工作原理解读
  6. Who is the best at Dataset X?
  7. 一个显示器分两个屏幕_桌面改造计划2.0:一个显示器不够那就两个,桌面好物分享...
  8. Sketch UX套件,用于线框图和原型制作
  9. 浅谈python的import
  10. 学python需要什么基础-学习python需要什么基础
  11. idea在编辑界面上显示多个文件
  12. 计算机电脑哪个是复位键,电脑一键还原按哪个键
  13. 买土豆的故事(转中外管理)
  14. 禁止苹果浏览器Safari将数字识别成电话号码的方法
  15. Help Hanzo(LightOJ - 1197)(欧拉筛 + 思维)
  16. 基于PLC的烟草真空回潮控制系统设计
  17. 【实例】用PHP制作一个简单的日历
  18. 我跨过山和大海,穿过人山人海,只为寻找到你
  19. 金蝶协同办公平台任意文件下载漏洞(无需登录)
  20. 随手科技企业入选互联网金融协会增选常务理事,消除随手记投资靠谱吗

热门文章

  1. Hyperledger Fabric从源码分析背书提案过程
  2. 手机锁屏后有时收不到微信通知,有时又能收到是怎么回事?
  3. 求复杂多边形面积的算法
  4. 【会议通知】关于召开第十届全国生物信息学与系统生物学学术大会的重要补充通知(第二轮)...
  5. Remix 以太坊Solidity IDE搭建与初步使用
  6. format code appears twice
  7. HMI(人机界面)工业自动化中最前沿的解决方案
  8. 国际网络收款工具Paypal注册图文教程
  9. Ubuntu系统下OpenCV使用实例(虚拟机获取摄像头权限)
  10. datastage7.5.1.A License 及 下载地址 Download