当前位置:网站首页>借问变量何处存,牧童笑称用指针,Go lang1.18入门精炼教程,由白丁入鸿儒,go lang类型指针(Pointer)的使用EP05
借问变量何处存,牧童笑称用指针,Go lang1.18入门精炼教程,由白丁入鸿儒,go lang类型指针(Pointer)的使用EP05
2022-08-08 20:07:00 【InfoQ】
指针声明
var var_name *var-type
var ip *int /* 指向整型*/
var fp *float32 /* 指向浮点型 */
package main
import "fmt"
func main() {
var a int = 20 /* 声明实际变量 */
var ip *int /* 声明指针变量 */
ip = &a /* 指针变量的存储地址 */
fmt.Printf("a 变量的地址是: %x\n", &a)
/* 指针变量的存储地址 */
fmt.Printf("ip 变量的存储地址: %x\n", ip)
/* 使用指针访问值 */
fmt.Printf("*ip 变量的值: %d\n", *ip)
}
package main
import "fmt"
func main() {
mystr := "字符串"
myint := 1
mybool := false
myfloat := 3.2
fmt.Printf("type of &mystr is :%T\n", &mystr)
fmt.Printf("type of &myint is :%T\n", &myint)
fmt.Printf("type of &mybool is :%T\n", &mybool)
fmt.Printf("type of &myfloat is :%T\n", &myfloat)
}
type of &mystr is :*string
type of &myint is :*int
type of &mybool is :*bool
type of &myfloat is :*float64
空指针
if(ptr != nil) /* ptr 不是空指针 */
if(ptr == nil) /* ptr 是空指针 */
package main
import "fmt"
func main() {
x := "字符串"
var ptr *string
fmt.Println("ptr is ", ptr)
ptr = &x
fmt.Println("ptr is ", ptr)
}
ptr is <nil>
ptr is 0xc00003c250
指针操作
package main
import (
"fmt"
)
func main() {
b := 255
a := &b
fmt.Println("address of b is", a)
fmt.Println("value of b is", *a)
}
address of b is 0xc000014088
value of b is 255
package main
import (
"fmt"
)
func main() {
b := 255
a := &b
fmt.Println("address of b is", a)
fmt.Println("value of b is", *a)
*a++
fmt.Println("new value of b is", b)
}
address of b is 0xc0000aa058
value of b is 255
new value of b is 256
package main
import (
"fmt"
)
func change(val *int) {
*val = 55
}
func main() {
a := 58
fmt.Println("value of a before function call is", a)
b := &a
change(b)
fmt.Println("value of a after function call is", a)
}
value of a before function call is 58
value of a after function call is 55
package main
import (
"fmt"
)
func change(val int) {
val = 55
}
func main() {
b := 58
change(b)
fmt.Println("value of a after function call is", b)
}
value of a after function call is 58
package main
import (
"fmt"
)
func modify(arr *[3]int) {
(*arr)[0] = 90
}
func main() {
a := [3]int{89, 90, 91}
modify(&a)
fmt.Println(a)
}
[90 90 91]
package main
import (
"fmt"
)
func modify(sls []int) {
sls[0] = 90
}
func main() {
a := [3]int{89, 90, 91}
modify(a[:])
fmt.Println(a)
}
[90 90 91]
package main
import "fmt"
func main() {
var a int
var ptr *int
var pptr **int
a = 3000
/* 指针 ptr 地址 */
ptr = &a
/* 指向指针 ptr 地址 */
pptr = &ptr
/* 获取 pptr 的值 */
fmt.Printf("变量 a = %d\n", a)
fmt.Printf("指针变量 *ptr = %d\n", *ptr)
fmt.Printf("指向指针的指针变量 **pptr = %d\n", **pptr)
}
变量 a = 3000
指针变量 *ptr = 3000
指向指针的指针变量 **pptr = 3000
package main
import "fmt"
func main() {
var a int
var ptr *int
var pptr **int
a = 3000
/* 指针 ptr 地址 */
ptr = &a
/* 指向指针 ptr 地址 */
pptr = &ptr
/* 获取 pptr 的值 */
fmt.Printf("变量 a = %d\n", a)
fmt.Printf("指针变量 *ptr = %d\n", *ptr)
fmt.Printf("指向指针的指针变量 **pptr = %d\n", **pptr)
**pptr = 200
fmt.Printf("变量 a = %d\n", a)
fmt.Printf("指针变量 *ptr = %d\n", *ptr)
fmt.Printf("指向指针的指针变量 **pptr = %d\n", **pptr)
}
变量 a = 3000
指针变量 *ptr = 3000
指向指针的指针变量 **pptr = 3000
变量 a = 200
指针变量 *ptr = 200
指向指针的指针变量 **pptr = 200
结语
边栏推荐
- Codeforces Round #722 (Div. 2)
- WPF主窗体调用 User32的SetWindowPos 设置窗体置顶会导致与其他窗体抢夺焦点的问题
- 一文教你普罗米修斯Prometheus的基础应用
- CAXA PLM云商店登榜,为制造企业数字化转型“保驾护航”
- 文档管理系统对于企业来说有哪些作用?
- Why Manufacturing Companies Should Deploy Digital Factory Systems
- 1259 Alice and Bob
- golang流程控制:if分支、switch分支和fallthrough switch穿透
- Categorized input and output, Go lang1.18 introductory refining tutorial, from Bai Ding to Hongru, go lang basic data types and input and output EP03
- 电脑win键没有反应(最全方案)
猜你喜欢
随机推荐
CAXA PLM云商店登榜,为制造企业数字化转型“保驾护航”
How can recommender systems be trusted?A review of the latest "Trusted Recommender System" from Rutgers University, a 43-page pdf explaining the composition and technology of trusted RS
Salesforce开发之 apex操作批准过程(Approval Process)
PyTorch入门:(一)数据加载
技术分享 | 接口自动化测试之JSON Schema模式该如何使用?
挖财学堂帮开通的证券账户是真的吗?安全吗
正则表达式的限定符、或运算符、字符类、元字符、贪婪/懒惰匹配
黑猫带你学Makefile第8篇:uboot/kernel中的makefile基本语法与流程
Experience Sharing | A low-cost and fast-paced approach to building an enterprise knowledge management system
1088 N的阶乘
wps表格怎么设置公式自动计算?wps表格设置公式自动计算的方法
西湖大学鞠峰组招聘【塑料降解 / 污水工程 / 微生物学】方向博士后和科研助理...
Linux下使用kill杀不死Mysql进程一直杀不死的问题解决方案
分门别类输入输出,Go lang1.18入门精炼教程,由白丁入鸿儒,go lang基本数据类型和输入输出EP03
Codeforces Round #725 (Div. 3)
fillder4 keeps prompting the system proxy was changed, watch me solve it
梅科尔工作室OpenHarmony设备开发培训笔记-第六章学习笔记
openEuler 资源利用率提升之道02:典型应用下的效果
OpenEuler's Ways to Improve Resource Utilization 02: Effects under Typical Applications
WPF主窗体调用 User32的SetWindowPos 设置窗体置顶会导致与其他窗体抢夺焦点的问题