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 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败