指针转换(C# 编程指南)

原文地址:https://msdn.microsoft.com/zh-cn/library/etb4xxec.aspx

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

发件人

目标

任何指针类型

void*

null

任何指针类型

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

发件人

目标

任何指针类型

所有其他指针类型

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

任何指针类型

任何指针类型

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

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

C#
 
  // compile with: /unsafe

C#
 
  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
    */

 

转载于:https://www.cnblogs.com/Arlar/p/6040468.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值