英文:
golang has pointer's pointer or pointer's reference feature?
问题
嗨,如果你需要改变指针指向的位置而不是改变指针指向的值,你可以在Golang中怎么做呢?我知道在C++中可以使用引用来实现这个功能,就像这样:
func myFunc(ptr **Type) {
*ptr = anotherPointer
}
func main() {
var ptr *Type = &someValue
myFunc(&ptr) // ptr被移动
}
或者在C中可以使用指针的指针:
func myFunc(ptrsptr ***Type) {
**ptrsptr = anotherPointer
}
func main() {
var ptr *Type = &someValue
myFunc(&ptr) // ptr被移动
}
我想知道Golang是否有这个方便的特性,如果没有,那么唯一的方法是在函数的返回值中设置指针的新值。
英文:
Hi everyone in Golang what will you do if you need to change the pointer (change where the pointer points to rather than change the value where this pointer points to). I know it is really easy in C++ by using reference, like"
void myFunc(Type*& ptr)
{
ptr = anotherPointer;
}
int main
{
Type* ptr = &someValue;
myFunc(ptr); // ptr is moved
}
Or equivalently in C, use pointer's pointer:
void myFunc(Type** ptrsptr)
{
*ptrsptr = anotherPointer;
}
int main
{
Type* ptr = &someValue;
myFunc(&ptr); // ptr is moved
}
I wonder if Golang has this neat feature, or if not, the only way is to set at function's return?
答案1
得分: 5
你可以使用指向指针的指针,就像在C语言中一样。
package main
import "fmt"
type Type struct{}
var anotherPointer = &Type{}
func myFunc(ptrsptr **Type) {
*ptrsptr = anotherPointer
}
func main() {
ptr := &Type{}
fmt.Printf("%p\n", ptr)
myFunc(&ptr) // ptr is moved
fmt.Printf("%p\n", ptr)
}
链接:http://play.golang.org/p/vE-3otpKkb
英文:
You can use a pointer to a pointer, just like in C
http://play.golang.org/p/vE-3otpKkb
package main
import "fmt"
type Type struct{}
var anotherPointer = &Type{}
func myFunc(ptrsptr **Type) {
*ptrsptr = anotherPointer
}
func main() {
ptr := &Type{}
fmt.Printf("%p\n", ptr)
myFunc(&ptr) // ptr is moved
fmt.Printf("%p\n", ptr)
}
答案2
得分: 0
下面的示例将仅更改变量的值:
package main
import "fmt"
func main() {
value := 200
var p1 *int = &value
var p2 **int = &p1
fmt.Printf("更新前变量的值为 %v,指针的地址为:%p\n", *p1, p1)
*p1 = 300
fmt.Printf("通过 p1 更新后变量的值为 %v,指针的地址为:%p\n", *p1, p1)
**p2 = 400
fmt.Printf("通过 p2 更新后变量的值为 %v,指针的地址为:%p\n", *p1, p1)
}
下面的代码将更改指针的值,并指向新的地址:
package main
import "fmt"
func changePointer(newP **int) {
val := 500
*newP = &val
}
func main() {
value := 200
var p1 *int = &value
fmt.Printf("更新前变量的值为 %v,指针的地址为:%p\n", *p1, p1)
changePointer(&p1)
fmt.Printf("更新后变量的值为 %v,指针的地址为:%p\n", *p1, p1)
}
英文:
The below example will change the value of variable only:
package main
import "fmt"
func main() {
value := 200
var p1 *int = &value
var p2 **int = &p1
fmt.Printf("Value of variable before updating %v and address of pointer is: %p\n", *p1, p1)
*p1 = 300
fmt.Printf("Value of variable after updating by p1 %v and address of pointer is: %p\n", *p1, p1)
**p2 = 400
fmt.Printf("Value of variable after updating by p2 %v and address of pointer is: %p\n", *p1, p1)
}
The below code will change the pointer value and also point to new address:
package main
import "fmt"
func changePointer(newP **int) {
val := 500
*newP = &val
}
func main() {
value := 200
var p1 *int = &value
fmt.Printf("Value of variable before updating %v and address of pointer is: %p\n", *p1, p1)
changePointer(&p1)
fmt.Printf("Value of variable after updating %v and address of pointer is: %p\n", *p1, p1)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论