【问题标题】:use value of transfer(by-value?) between function calls with pointers to stackvariables在带有指向堆栈变量的指针的函数调用之间使用传输值(按值?)
【发布时间】:2015-01-17 18:02:19
【问题描述】:

函数调用之间的“转移价值”(按价值?不是百分百确定英文术语)是什么意思。给我这样做的例子,假设我使用指向堆栈变量的指针。

我真的不明白“转移价值”的概念。函数应该返回另一个函数什么?

如果我使用下面示例中的指针,我只是传输指针地址?那么如何使用带有指向堆栈变量的指针的传输值呢?

void fun1(){
  int x = 44;
  int *y = &x;
}

void fun2(){
  int *y;
  }

从第一个答案:

   void fun1(){
        int x = 44;
        fun2( &x );
        printf( "%d\n", x );       // prints 55
    }

    void fun2( int *value ){
        printf( "%d\n", *value );  // prints 44
        *value = 55;               // changes the value of `x` in `fun1`

}

对我来说,似乎我只是将一个指向堆栈变量 (x) 的指针转移到 fun2?所以实际的问题是:如何使用指向堆栈变量的指针在函数调用之间传输值?

您可能已经回答了这个问题?但是我想确定这一点,如果我做对了,我想确定一下,所以到目前为止我的想法是:我首先将一个指向堆栈变量 x 的指针从 fun1 发送到 fun2。当 fun2 被调用时,我通过 *value = 55 将 int x = 44 的值更新为 55,并且 *value 是一个指向堆栈变量的指针,所以我实际上在指向 a 的指针的帮助下更新了变量 x 的值堆栈变量。但是,我在哪里使用这种指向堆栈变量的指针技术在函数之间传输值。我是否在函数之间传递值?我不这么认为,如果我这样做了,我应该向另一个函数返回一些东西。现在看来我只是在函数调用之间更新了一个变量?但也许这个问题已经回答了?但是对于函数调用之间传递值是什么意思,我还是有点困扰。

【问题讨论】:

    标签: c variables pointers stack pass-by-value


    【解决方案1】:

    另一种思考pass by valuepass by reference 的方式是了解正在传递的内容。当您调用function (int x) 时,您会将x 的副本传递给function。无论您对function 中的x 做什么,xfunction 之外的值都保持不变——因为您只是将x副本传递给function .

    另一方面,当您调用function (int *x) 时,您将x地址x引用)传递给function .这就是为什么& 通常被称为the address of 运算符。无论你对function 中的x 做什么,现在都会更改x 指向的内存。所以xfunction之外的值也改变了,因为你改变了地址&x所指向的内存

    以下是您的函数的简短注释示例:

    #include <stdio.h>
    
    /* value is copy of variable 'x' holding its value  */
    void fun2_by_value ( int value )
    {
        printf( "%-17s - %d\n", __func__, value );  /* prints 44    */
        value = 55;   /* values is a copy of x from fun1_by_value   */
    }
    
    void fun1_by_value ()
    {
        int x = 44;
        fun2_by_value (x);          /* passes copy of 'x's value    */
        printf ( "%-17s - %d\n", __func__, x );     /* prints 44    */
    }
    
    /* value is pointer to memory containing 'x' */
    void fun2_by_reference ( int *value )
    {
        printf ( "%-17s - %d\n", __func__, *value ); /* prints 44   */
        *value = 55;   /* changes mem pointed to by address 'value' */
    }
    
    void fun1_by_reference ()
    {
        int x = 44;
        fun2_by_reference (&x);         /* passes address of 'x'    */
        printf ( "%-17s - %d\n", __func__, x );     /* prints 55    */
    }
    
    int main () {
    
        printf ("\nCalling 'fun1_by_value ();'\n\n");
        fun1_by_value ();
    
        printf ("\nCalling 'fun1_by_reference ();'\n\n");
        fun1_by_reference ();
    
        printf ("\n");
    
        return 0;
    }
    

    输出:

    $ ./bin/byrbyv
    
    Calling 'fun1_by_value ();'
    
    fun2_by_value     - 44
    fun1_by_value     - 44
    
    Calling 'fun1_by_reference ();'
    
    fun2_by_reference - 44
    fun1_by_reference - 55
    

    【讨论】:

      【解决方案2】:

      如果您希望fun2 能够更改fun1 中的变量x,那么您可以像这样将指向x 的指针传递给fun2

      // This code demonstrates "pass by address" which (for the C programming
      // language) is the same as "pass by reference". 
      
      void fun1(){
          int x = 44;
          fun2( &x );
          printf( "%d\n", x );       // prints 55
      }
      
      void fun2( int *value ){
          printf( "%d\n", *value );  // prints 44
          *value = 55;               // changes the value of `x` in `fun1`
      }
      

      如果您将x 作为参数而不是x 的地址传递,那么fun2 将无法更改x 的值fun1

      // This code demonstrates "pass by value". fun2 is given the value of x but 
      // has no way to change fun1's copy of x.
      
      void fun1( void ){
          int x = 44;
          fun2( x );
          printf( "%d\n", x );      // prints 44
      }
      
      void fun2( int value ){
          printf( "%d\n", value );  // prints 44
          value = 55;               // has no effect on `x` in `fun1`
      }
      

      【讨论】:

      • OP 不知道这些叫什么。在你的回答中提到这两种方式
      • @CoolGuy 谢谢,做了一些改变。现在看起来还好吗?
      猜你喜欢
      • 2020-01-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-13
      • 2021-12-04
      • 1970-01-01
      • 2010-12-19
      相关资源
      最近更新 更多