zoukankan      html  css  js  c++  java
  • C#指针转换

    下表显示了预定义的隐式指针转换。 隐式转换可能在多种情形下发生,包括调用方法时和在赋值语句中。

     

    From

    To

    任何指针类型

    void*

    null

    任何指针类型

    显式指针转换用于在不存在隐式转换时通过使用强制转换表达式来执行转换。 下表显示了这些转换。

     

    From

    To

    任何指针类型

    所有其他指针类型

    sbyte、byte、short、ushort、int、uint、long 或 ulong

    任何指针类型

    任何指针类型

    sbyte、byte、short、ushort、int、uint、long 或 ulong

    示例
     
     

    在下面的示例中,一个指向 int 的指针被转换为指向 byte 的指针。 注意,该指针指向变量的最低地址字节。 连续递增该结果直到达到 int 的大小(4 字节),即可显示变量的剩余字节。

    class ClassConvert
    {
        static void Main() 
        {
            int number = 1024;
    
            unsafe 
            {
                // Convert to byte:
                byte* p = (byte*)&number;
    
                System.Console.Write("The 4 bytes of the integer:");
    
                // Display the 4 bytes of the int variable:
                for (int i = 0 ; i < sizeof(int) ; ++i)
                {
                    System.Console.Write(" {0:X2}", *p);
                    // Increment the pointer:
                    p++;
                }
                System.Console.WriteLine();
                System.Console.WriteLine("The value of the integer: {0}", number);
    
                // Keep the console window open in debug mode.
                System.Console.WriteLine("Press any key to exit.");
                System.Console.ReadKey();
            }
        }
    }
        /* Output:
            The 4 bytes of the integer: 00 04 00 00
            The value of the integer: 1024
        */
  • 相关阅读:
    Search a 2D Matrix
    binary search bug
    Find Minimum in Rotated Sorted Array II
    Search in Rotated Sorted Array 【新思路】
    Find Peak Element
    Find Minimum in Rotated Sorted Array
    DFS判断连通图
    分支限界法解决01背包问题
    python中ndarray和matrix
    python对数组缺失值进行填充
  • 原文地址:https://www.cnblogs.com/2Yous/p/4887930.html
Copyright © 2011-2022 走看看