Go by Example: Pointers

Go语言支持指针,允许你在程序里面通过引用传递变量值。

package main

import "fmt"

// 我们用两个函数:zeroval和zeroptr来演示指针的用法
// zeroval函数有一个int类型参数,这个时候传递给函数的是变量的值。
//zeroval从调用它的哪个函数中得到一个ival的拷贝值。
func zeroval(ival int) {
    ival = 0
}

// 相比之下zeroptr函数的参数是int类型指针,这就意味着传递给该函数的一个int的指针。
//这个 *iptr在函数体内从它的内存地址解析获得这个地址对应的当前值。
// 对于解引用的指针进行赋值操作会改变这个指针引用的真实地址的值。
func zeroptr(iptr *int) {
    *iptr = 0
}

func main() {
    i := 1
    fmt.Println("initial:", i)

    zeroval(i)
    fmt.Println("zeroval:", i)

    // 通过&操作符用来取得变量的内存地址
	//例如: &i为变量i的指针
    zeroptr(&i)
    fmt.Println("zeroptr:", i)

    // 指针类型也可以被打印输出的
    fmt.Println("pointer:", &i)
}

输出

$ go run pointers.go
initial: 1
zeroval: 1
zeroptr: 0
pointer: 0x42131100

其中zeroval 在 main 函数中不能改变 i 的值,但是zeroptr 可以,因为它有一个这个变量的内存地址的引用。

下一个例子: Go by Example:Structs

英文原文

欢迎扫描下面的二维码关注我的微信公众号:codemanship(码术)

时间: 2024-10-29 20:49:13

Go by Example: Pointers的相关文章

LeetCode OJ:Populating Next Right Pointers in Each Node II(指出每一个节点的下一个右侧节点II)

Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tree could be any binary tree? Would your previous solution still work? Note: You may only use constant extra space. For example,Given the following binary tr

[leetcode] Populating Next Right Pointers in Each Node

Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; } Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL. Initially, al

leetcode--Populating Next Right Pointers in Each Node II

Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tree could be any binary tree? Would your previous solution still work? Note: You may only use constant extra space. For example,Given the following binary tr

leetcode 117 Populating Next Right Pointers in Each Node II ----- java

Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tree could be any binary tree? Would your previous solution still work? Note: You may only use constant extra space. For example,Given the following binary tr

【树】Populating Next Right Pointers in Each Node

题目: Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; } Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL. Initially

More Effective C++ 条款28 Smart Pointers(智能指针)

1. 智能指针(如标准库的auto_ptr,shared_ptr,weak_ptr,boost的scoped_ptr等)主要用于动态内存的管理,同时提供给用户与内置指针一样的使用方法,本条款主要涉及智能指针在构造与析构,复制和赋值,解引等方面的注意点,而非智能指针的实现细节. 2. 智能指针的构造,赋值,析构 智能指针的copy constructor,assignment operator,destructor对应于不同的观念而有不同的实现,主要有三种选择: 1).不允许对象的共享,在调用co

LeetCode——Populating Next Right Pointers in Each Node

Description: Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; } Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL.

More Effective C++ 条款1 仔细区别pointers和references

1. 初始化的区别:有空指针(NULL),但没有空引用,和const变量一样,引用一旦定义就必须和对象绑定.(当然char* pc=0;char& rc=*pc;也合法但无意义) 由此造成的影响: 1) dynamic_cast,对于指针的down_cast,如果失败就返回空指针,但由于没有"空引用"的说法,所以对于引用的down_cast如果失败会抛出一个bad_cast异常. 2) 由于对于引用的使用不需要测试其有效性,而对于指针的使用往往要测试其是否为空. 2. 赋值的区

Populating Next Right Pointers in Each Node -- leetcode

Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; } Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL. Initially, al

116.Populating Next Right Pointers in Each Node

Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; } Populate each next pointer to point to its next right node. If there is nonext right node, the next pointer should be set to NULL. Initially, all