zoukankan      html  css  js  c++  java
  • 66_Go基础_1_33 指针

     1 package main
     2 
     3 import "fmt"
     4 
     5 func main() {
     6     /*
     7         指针:pointer
     8             存储了另一个变量的内存地址的变量。
     9 
    10     */
    11 
    12     // 1.定义一个int类型的变量
    13     a := 10
    14     fmt.Println("a的数值是:", a)     // 10
    15     fmt.Printf("%T\n", a)        // int
    16     fmt.Printf("a的地址是:%p\n", &a) //0xc0000140a8
    17 
    18     // 2.创建一个指针变量,用于存储变量a的地址
    19     var p1 *int
    20     fmt.Println(p1) // <nil>,空指针
    21     p1 = &a
    22     fmt.Println("p1的数值:", p1)       // p1的数值: 0xc0000140a8
    23     fmt.Printf("p1自己的地址:%p\n", &p1) // p1自己的地址:0xc000006030
    24     fmt.Println("p1的数值,是a的地址,该地址存储的数据:", *p1)
    25     // p1的数值,是a的地址,该地址存储的数据: 10
    26 
    27     // 3.操作变量,更改数值 ,并不会改变地址
    28     a = 100
    29     fmt.Println(a)
    30     fmt.Printf("%p\n", &a) // 0xc0000140a8
    31 
    32     // 4.通过指针,改变变量的数值
    33     *p1 = 200
    34     fmt.Println(a) // 200
    35 
    36     // 5.指针的指针
    37     var p2 **int
    38     fmt.Println(p2)                     // <nil>
    39     p2 = &p1                            //
    40     fmt.Printf("%T,%T,%T\n", a, p1, p2) // int, *int, **int
    41     fmt.Println("p2的数值:", p2)           // p2的数值: 0xc000006030  p1的地址
    42     fmt.Printf("p2自己的地址:%p\n", &p2)     // p2自己的地址:0xc000006038
    43     fmt.Println("p2中存储的地址,对应的数值,就是p1的地址,对应的数据:", *p2)
    44     // p2中存储的地址,对应的数值,就是p1的地址,对应的数据: 0xc0000140a8
    45     fmt.Println("p2中存储的地址,对应的数值,再获取对应的数值:", **p2)
    46     // p2中存储的地址,对应的数值,再获取对应的数值: 200
    47 
    48 }
  • 相关阅读:
    微信开发-如何自定义页面分享元素
    nginx实现日志按天切割
    JS兼容IE浏览器的方法
    mysql 索引过长1071-max key length is 767 byte
    playframework1.x的eclipse插件开源-playtools
    开放平台-web实现人人网第三方登录
    开放平台-web实现QQ第三方登录
    bash shell执行方式
    pushd和popd
    What do cryptic Github comments mean?
  • 原文地址:https://www.cnblogs.com/luwei0915/p/15629941.html
Copyright © 2011-2022 走看看