c - Operations with pointers in linked lists
问题描述
Generally, I know that a pointer stores the memory address of another value located in computer memory, for example:
int firstvalue= 5
int * p1;
p1 = &firstvalue; // p1 = address of firstvalue
What happens if we define an operation like the following in a linked list? Does *current=*list
means that the value pointed to by current equals to the value pointed to by list? And what does it mean if we define ecur=current
?
int function(struct list_t **list){
struct list_t *ecur=NULL;
struct list_t *current=*list;
ecur=current;
}
Update:
What does it do *list=remove(*list, param1, param2)
? And why is that?
remove
is a function that returns a modified list of list
.
Update 2:
Why do we need to define a pointer to pointer in order to modify the list? Is *list
a pointer to pointer?
解决方案
该变量list
是一个指向结构 list_t 的指针的指针。如果我们(仅作为示例)假设该结构位于地址 2000 并且未命名指针位于地址 1000 它将如下所示:
然后你有添加两个新变量的初始化。两者都作为指向 struct list_t 的指针。
struct list_t *ecur=NULL;
struct list_t *current=*list;
所以现在图片变成了:
请注意,current
它与中间的“some-pointer”具有相同的值,因为它*list
被分配给current
.
然后你有任务:
ecur=current;
这意味着ecur
获得与图片相同的值current
并给出图片:
更新:它有什么作用
*list=remove(*list, param1, param2)
?
它改变了图片中间的“一些指针”的值。例如,如果remove
函数删除链表中的第一个元素,则需要这样做。
推荐阅读
- ruby-on-rails - 如何在 config/application.rb 文件中修改数字
- ios - 如何用 Core Data 中的数据填充 TextField 并更新更改?
- jquery - 循环遍历一组 DIv 并将它们的 innerHTML 显示为 H 标签?
- vim - 创建一个词汇笔记本 [Vim]
- android - 当我尝试从 Android 中的 AWS Dyanamodb 读取数据时出现 NullPointer 异常错误?
- c - 带有一个输入的 strcmp 的分段错误
- python - 构造函数重载python
- python - 使用 Python 和 Selenium,我将如何绕过 2 因素身份验证?
- r - 在 macOS Catalina 上为 Jupyter 提供内核时出现 IRkernel::installspec() 错误
- javascript - 最后一个选项卡下一个按钮将其重定向到主页