zoukankan      html  css  js  c++  java
  • 【Golang】快速复习指南QuickReview(五)——指针

    指针

    指针也就是内存地址,指针变量是用来存放内存地址的变量。学习C语言,C++经常使用指针,Golang中也是指针使用的高频语言,C#几乎没怎么用过。但是不代表C#中不能使用指针。只是设计者并不希望开发者在不熟练的情况下使用指针,引发安全问题。

    1.C#中的指针

    1.1 修改配置

    C#默认是不允许使用指针,强行dotnet run会报错:

    Unsafe code may only appear if compiling with /unsafe
    

    需要再.csproj中增加如下配置,以允许不安全的代码(指针)

    <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
        <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
    </PropertyGroup>
    

    1.2 unsafe

    unsafe static void TestPoint()
    {
        int i = 10;
    
        //指针变量
        int* iptr = &i;
        Console.WriteLine(*iptr);//指针取值(根据指针去内存取值)
    }
    

    2.Golang的指针

    2.1 取地址&

    i := 10
    iptr := &i //取i的地址赋值给iptr
    fmt.Printf("type of iptr:%T
    ", iptr)
    fmt.Printf("value of b:%v
    ", iptr)
    
    type of iptr:*int
    value of b:0xc000014118
    

    2.2 地址取值

    fmt.Printf("value of iptr's address :%v
    ", *iptr) //指针取值(根据指针去内存取值)
    
    value of iptr's address :10
    

    2.3 new

    a := new(int) //分配一个内存,并把内存地址赋值给a变量
    fmt.Printf("%T
    ", a)
    *a = 10
    fmt.Printf("%v
    ", a)
    fmt.Printf("%v
    ", *a)
    
    *int
    0xc0000a2108
    10
    

    指针就这么点内容,配合后面的结构体,指针将会发挥大作用。

    再次强调:这个系列并不是教程,如果想系统的学习,博主可推荐学习资源。

  • 相关阅读:
    关于scrollTop的那些事
    document.documentElement.clientHeight||document.documentElement.scrollHeight
    用JS查看修改CSS样式(cssText,attribute('style'),currentStyle,getComputedStyle)
    Pygame安装教程
    Python基础知识:测试代码
    Python基础知识:文件和异常
    Python基础知识:类
    Python基础知识:字典
    Python基础知识:while循环
    Python基础知识:列表
  • 原文地址:https://www.cnblogs.com/RandyField/p/14025419.html
Copyright © 2011-2022 走看看