dtmu88600 2015-06-02 08:08
浏览 37
已采纳

将指针或副本的参数传递给函数

I am contemplating on the Go pointers, passing variables as parameters to functions by value or by reference. In a book I have encountered a good example, which is the first code snippet below, on passing a pointer.

The first version is working as expected, in function that takes parameter of a pointer makes changes to the variable itself, not on a copy of it. But the second example below I am tinkering with works on a copy of it. I have thought they should behave equivalently, and second one to work on the variable passed as parameter, not on copy of it.

Essentially, what these two versions of the function is behaving different?

version in the book, passing parameters by reference:

package main

import (
    "fmt"
)
// simple function to add 1 to a
func add1(a *int) int {
    *a = *a+1 // we changed value of a
    return *a // return new value of a
}

func main() {
    x := 3

    fmt.Println("x = ", x)  // should print "x = 3"

    x1 := add1(&x)  // call add1(&x) pass memory address of x

    fmt.Println("x+1 = ", x1) // should print "x+1 = 4"
    fmt.Println("x = ", x)    // should print "x = 4"
}

my alternative tinkering version, passing pointer parameter:

package main

import (
    "fmt"
)
// simple function to add 1 to a
func add1(a int) int {
    p := &a
    *p = *p+1 // we changed value of a
    return *p // return new value of a
}
func main(){
    fmt.Println("this is my go playground.")

        x := 3

        fmt.Println("x = ", x)  // should print "x = 3"

        x1 := add1(x)  // call add1(&x) pass memory address of x

        fmt.Println("x+1 = ", x1) // should print "x+1 = 4"
        fmt.Println("x = ", x)    // should print "x = 4"   
}
  • 写回答

2条回答 默认 最新

  • dongxian3852 2015-06-02 08:14
    关注

    Why should they behave equivalently? In the first example you pass a pointer (to int), in the second example you pass an int value.

    What happens in the 2nd example is that you pass an int value. Parameters in functions work like local variables. A local variable called a of type int will be created, and will be initialized with the value you passed (3 which is the value of x). This local variable is addressable like any other variables.

    You take its address (p := &a) and you increment the value pointed by this pointer (which is variable a itself). And you return the value pointed by it which is the incremented value of a.

    Outside from where you called this add1() function, the value of x is not modified because only the local copy a was modified.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog