结构和指向指针的指针 [英] Struct and pointer to pointer

查看:151
本文介绍了结构和指向指针的指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习链接列表,以及如何在C中使用结构和指针创建它们.我在下面有一个例子.根据我的理解,被称为push()传递了头节点所在的结构的开始存储位置作为参数. push()函数的参数将struct节点作为指向指针的指针,因此将其作为引用而不是实际副本传递.因此,结构节点** headref的第一个指针只是指向头节点的内存位置的指针,第二个指针指向该值,即该头节点指向的下一个内存位置.我们通过在struct节点内分配一些内存来创建一个名为newnode的新节点.然后,我们在此节点内创建一个int类型的数据.

I am learning about linked lists and how to create them in C with structs and pointers. I have an example below. From my understanding the called push() passes the beginning memory location of our struct where the head node lies as an argument. The parameter of our push() function takes a struct node as a pointer to pointer so it is passed as a reference, not an actual copy. So the first pointer of our struct node** headref is just a pointer to the memory location of our head node and the second pointer points to the value, which is the next memory location that the head node points to. We create a new node called newnode inside our struct node by assigning it some memory. We then create an int type data inside this node.

好吧,假设我说的都是正确的,下一部分就是我的困惑.

Okay assuming everything I said is correct, the next part is what i am confused on.

newNode->next= *headRef; 

据我了解,这行代码取消了对headref的引用,因此这只会使headref指向头节点.然后我们有一个指针操作,headref指向的操作也将是我们接下来的指针指向的操作.基于此,我们新节点(newnode)中的下一个指针将指向头指针.

This line from what i can understand dereferences the headref so this would just have the headref pointing to the head node. Then we have a pointer operation where what the headref is pointing to will then also be what our pointer next will be pointing to. Based on that the pointer next in our new node(newnode) will be pointing to the head pointer.

我也感到困惑的下一行是:

The next line which i am also confused on is:

*headRef = newNode;

已取消引用的headref指针所指向的对象(即头节点)现在将指向我们的newnode.

What the dereferenced headref pointer is pointing to, which is the head node, will be pointing now to our newnode.

基于这一点,应该有一个名为newnode的新节点,该节点具有int数据,并且下一个指针将我们的newnode链接到头部.然后,headref指针(或者是头节点?)将指向新节点.我知道这是不正确的,因为newnode的下一个指针应指向第二个节点,以便我们的newnode可以在结构中链接.我也不相信我在上面两行代码中理解了指针并取消了引用.

Based on that there should be a new node called newnode with an int data and a next pointer linking our newnode to the head. Then the headref pointer(or is it the head node?) will point to the new node. I know this isn't correct because the pointer next of our newnode should be pointing to the second node so our newnode can be linked in the struct. I also don't believe that i am understanding the pointer to pointer and dereferencing in the above two lines of code.

代码:

void Push(struct node** headRef, int data) {
  struct node* newNode = malloc(sizeof(struct node));

  newNode->data = data;
  newNode->next = *headRef;
  *headRef = newNode;
}

void PushTest(void) {
  struct node* head = BuildTwoThree(); // suppose this returns the list {2, 3}

  Push(&head, 1);
  Push(&head, 13);
  // head is now the list {13, 1, 2, 3} 
}

推荐答案

void Push(struct node** headRef, int data) {

headRef包含结构节点所在的地址的地址.它是 PushTest 函数中 head 变量的地址.

headRef contains an address of address where struct node is located. It is an address of the head variable in PushTest function.

struct node* newNode = malloc(sizeof(struct node));

在这里我们创建了一个newNode.它包含节点struct所在的内存地址.

Here we created a newNode. It contains an address of memory where node struct is located.

newNode->data = data;

将newNode的数据设置为传递给 Push 函数的 data 参数的值-确定.

set the data of newNode to the value of the data parameter passed into the Push function - OK.

newNode->next = *headRef;

newNode-> next 设置为位于 head 变量中的结构的地址.例如,如果我们写过

set the newNode->next to the address of the structure located in the head variable. For example, if we have written

 void Push(struct node* head, int data) {
 ...
 newNode->next = head;

接下来,我们需要将 head 变量更改为 newNode .如果 head 变量是通过引用传递的,我们可以像在C ++中那样简单地编写它:

Next, we need to change the head variable to the newNode. If head variable was passed by reference, we could simply write this, like in C++:

void Push(struct node* &head, int data) {
...
head = newNode;

但是在普通C语言中,我们必须传递 head 变量所在的地址,因此我们可以将指向我们在结构中创建的结构 newNode 的指针写入该地址. Push 函数:

But in plain C we have to pass the address where head variable is located, so we can write into that address the pointer to the structure newNode we created in the Push function:

*headRef = newNode;

等效于将newNode指针写入其地址位于headRef变量内的变量

您的逻辑中有一个问题:

And there is a problem in your logic:

push()函数的参数将struct节点作为指针 指向指针,以便将其作为参考而不是实际副本传递.

The parameter of our push() function takes a struct node as a pointer to pointer so it is passed as a reference, not an actual copy.

实际上,我们传递的是临时变量的副本,其中包含 node 变量的地址,而不是引用的地址. C ++中有一个 pass-by-reference 方法,它使用&符号声明传递给函数的变量是一个引用,而不是原始变量的副本.

Actually, we pass a copy of temporary variable that contains an address of node variable, not the reference. There is pass-by-reference method in C++, it uses ampersand to declare that the variable passed to function is a reference, not a copy of original variable.

这篇关于结构和指向指针的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆