【问题标题】:How to pass a struct by reference and change its value如何通过引用传递结构并更改其值
【发布时间】:2013-07-22 03:15:21
【问题描述】:

大家好,我的 C 代码有这个问题。 我正在实现一个堆栈,每当我弹出它时,它都会更改函数 pop 中的堆栈,但不会更改原始堆栈。 请帮忙。

这是我的代码

char pop(Node* top)
{
    char result ; 

    if (size == 0) // The stack is empty.
    {
        return '\0' ;
    }

    else //The stack contains at least one element .
    {
        result = top->opp ;
        top = top->next ;
    }

    size-- ;

    return result;
}

【问题讨论】:

  • 通过查看代码,我建议您进行一些编辑 1. 你不要删除在开始时传递给你的top,所以这是内存泄漏,2. 因为它是堆栈,所以你不要'不需要size,您可以简单地检查if(top != NULL) 表示堆栈不为空...

标签: c function pointers reference stack


【解决方案1】:

我们需要更多代码,但我会尝试:

我猜你是这样使用这个函数的:

char pop(Node* top)
{
    char result ; 

    if (size == 0) // The stack is empty.
    {
        return '\0' ;
    }

    else //The stack contains at least one element .
    {
        result = top->opp ;
        top = top->next ;
    }

    size-- ;

    return result;
}

int main()
{
   // this is the top of the stack
   Node    *stack; // let's say there already are some elements in this stack
   pop(stack);
   return 0;
}

问题是你想改变指针值,那么stack会指向栈顶。为此,您必须使用指向指针的指针,如下所示:

char pop(Node** top)
{
    char result ; 
    Node *ptr;

    ptr = *top;
    if (size == 0) // The stack is empty.
    {
        return '\0' ;
    }

    else //The stack contains at least one element .
    {
        result = (*top)->opp ;
        (*top) = (*top)->next ;
        // You should free the pointer, like user2320537 said in his answer.
        free(ptr);
    }

    size-- ;

    return result;
}

int main()
{
   // this is the top of the stack
   Node    *stack; // let's say there already are some elements in this stack
   pop(&stack); // You give the pointer address
   return 0;
}

【讨论】:

    【解决方案2】:

    试试char pop(Node** top)并在(*top)上操作

    【讨论】:

      【解决方案3】:

      你还需要释放top的当前位置...使用free as

      Node *ptr;
      
      ptr = top;
      
      if (size == 0) // The stack is empty.
          {
              return '\0' ;
          }
      
          else //The stack contains at least one element .
          {
              result = top->opp ;
              top = top->next ;
          }
      
          free(ptr); 
      

      ================================================ ==================

      称之为

      int main(){
       Node front = NULL:
      
       // place your code of PUSH here.
      
       pop(&front); // will call the function **pop**
      
      }
      

      }

      【讨论】:

        【解决方案4】:

        如果你改变一个变量的值,你将一个指针(它的地址)传递给一个函数 例如

        void increment(int *p) {
              p += 1;
        }
        

        类似地改变一个指针的值,你需要传递一个指向函数指针的指针

        char pop(Node **top) {
               char t;
               Node  *p;
            if( size == 0 ) {
                return '\0';
            } else {
                p = *top;
                t = p->op;
                (*top) = (*top)-> next;
                free(p);
                return t;
            }
        }
        

        【讨论】:

          【解决方案5】:

          请将顶部指针的引用发送给 pop 函数,例如“char pop(Node **top) { }”,并添加更改您的 else 块“top[0] = top[0]->next;”而不是“top = top->next ;”。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2014-12-13
            • 2010-12-21
            • 1970-01-01
            • 2015-11-14
            • 2013-06-04
            • 2012-09-20
            • 1970-01-01
            相关资源
            最近更新 更多