【问题标题】:How can I pass a single pointer to a structure, inside a function and modify that structure variable?如何在函数内部将单个指针传递给结构并修改该结构变量?
【发布时间】:2015-07-01 13:57:28
【问题描述】:

在下面的代码中,我可以从函数中修改 main 中使用的变量。

#include<stdio.h>

    int main()
    {
      int *a,b=10;
      a = &b;
      printf("%d\n",*a);
      ref(a);
      printf("%d\n",*a);
      return 1;
    }

    int ref(int *a)
    {
       int b = 98;
       *a = b;
       return 1;
    }

而在下面的代码中,我无法做到这一点。

我知道我们可以使用双指针从函数中修改主值。我也知道我们可以使用单指针进行修改,方法是将所需的地址返回给主,并以相同的数据类型在主中获取它。我只是想知道是否可以通过将 main 中的值作为参数传递给函数来修改 main 中的值,仅作为指向(结构)变量的单个指针。

注意:我已经用注释“//WC”表示了工作代码。如果有人能向我解释一下,将非常感激。

  //int insert(int data, Node **head) //WC
    int insert(int data, Node *head)
    {
       Node *temp, *run;
       temp = (Node *) malloc(sizeof(Node));
       temp->data = data;
       temp->next = NULL;

       //if(*head == NULL) //WC
       if(head == NULL)
       {  
          printf("1st node\n");
          //*head = temp; //WC
          *head = *temp;
       }
       else
       {
          printf("node after first\n");
          //run = *head //WC
          *run = *head;
          while(run->next != NULL)
          {
             run = run->next;
          }
          run->next = temp;
       }

       return 1;
    }
    int main()
    {
       Node *head;
       insert(10, head);
       insert(20, head);
       insert(30, head);
       insert(40, head);
       insert(50, head);

       return 1;
    }

【问题讨论】:

  • 在以后的版本中,if(head == NULL) {*head = *temp;}会出现段错误。
  • 使用单个*,您可以修改参数指向的内容,但不能修改指针本身。那是您需要 double** 的时候,然后您可以修改两者。如果你想修改它,你必须(如你所说)从函数返回一个新的指针值。
  • 同样,您的后一个版本*run = *head; 也会调用未定义的行为; run 不确定。坚持任一种你知道的工作方式。
  • 是的,我知道我的代码不可执行,我无法使其可执行。这就是为什么我发布问题@SouravGhosh
  • @Denise 你没明白我的意思。我想,你实际上是想写head = temp,不是吗?不需要取消引用。

标签: c pointers structure singly-linked-list


【解决方案1】:

如何将单个指针传递给函数内部的结构并修改该结构变量?

TL;DR :可以修改指针所指向的值,但不能修改传递的指针本身。

C 使用按值传递函数参数传递。您不能更改从被调用函数接收到的参数的值,并期望它在用作调用函数参数的变量中反映。

但是,如果是指针,您通常不会修改指针本身,而是修改它所指向的值。因此,该指针指向的更改值将在调用函数中反映。

【讨论】:

  • 你的意思是我可以修改 head 而不是 *head ?
【解决方案2】:

当你检查 head 是否为空(有 NULL 值)时,你需要检查 head (*head) 的内容,而不是 head 本身,因为这意味着它自己的地址。所以如果 (head == NULL),应该是 *head == Null。 head 表示指针头的内存地址,*head 表示该地址中保存的内容(指向的内容)。有了这个逻辑,*head = temp;是正确的,因为它将在 head 中保存动态分配的内存地址 -temp 的地址,但是后一个 (*head = *temp) 将尝试将 temp 的内容复制/保存到 head 这没有意义,因为 head 有无论节点的大小如何,都只能分配指针和 temp 的大小。我希望我至少能帮上一点忙,这是您的代码的工作版本:)

int insert(int data, Node **head) //WC, This is correct because the parameter **head takes address of the pointer to the first node if any. 
//int insert(int data, Node *head)
{
   Node *temp, *run;
   temp = (Node *) malloc(sizeof(Node));
   temp->data = data;
   temp->next = NULL;

   if(*head == NULL) //WC, checking if head has any address value inside (not its own address)
   {  
      printf("1st node\n");
      *head = temp; //WC, because temp has address of the allocated memory and you wanna hold that address as the head / first node.
   }
   else
   {
      printf("node after first\n");
      run = *head //WC
      //*run = *head; you can't use this, one reason is because head could point to any size of memory (e.g 10000kb) and run has a size of a pointer, just few bytes.  
      while(run->next != NULL)
      {
         run = run->next;
      }
      run->next = temp;
   }

   return 1;
}

(编辑:使用多指针可能会使阅读变得复杂,所以我宁愿使用以下结构定义)

typedef struct node* PNode; //Pointer to node
typedef struct node {
    int item;
    Pnode next;
} Node;

void insert(int data, PNode *head) {
    PNode temp, run = *head;
    temp = (PNode)malloc(sizeof(Node));
    temp->data = data;
    temp->next = NULL;
    if (run == NULL){
        *head = temp;
    }//first node
    else{
        while (1) {
            if (run->next == NULL) {
                run->next = temp;
                break;
            }
            run = run->next;
        }
    }
}

【讨论】:

  • 你编译的??它为以下 *head = temp; 和 run = *head 引发不兼容的类型错误
  • 和invalid operands to binary == 为*head == NULL
  • 你能在这里(节点)发布你的结构定义吗?
  • 我刚刚编辑了它,我的错 :) *head 应该是 **head 因为参数中的 *head 是第一个节点的地址,但 **head 是指向的指针的地址第一个节点。
  • 正如我已经说过的,我知道使用双指针执行相同操作的方法。我所需要的是,我可以在函数中更改我的 *var 并使其在 int 数据类型的情况下反映在 main 中。我不知道在结构的情况下我应该如何做。如果可以@Lukas,请帮助我。无论如何,感谢您的解释。
猜你喜欢
  • 1970-01-01
  • 2012-01-12
  • 2020-05-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-21
  • 2013-11-15
  • 1970-01-01
相关资源
最近更新 更多