c#中指针操作结构

 
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace 不安全代码
  5. {
  6.     unsafe class 指针访问结构
  7.     {
  8.         public static void Main()
  9.         {
  10.             Mystruct* pStruct;
  11.             Mystruct mystr = new Mystruct();
  12.             //指针指向结构
  13.             pStruct = &mystr;
  14.             ( *pStruct ).x = 500;
  15.             ( *pStruct ).y = 12.0f;
  16.             // *pStruct ).x写法过于复杂,c#提供了指针预算符 ->
  17.             Console.WriteLine( pStruct ->x );
  18.             Console.WriteLine( pStruct->y );
  19.             //也可以把指针直接指向结构内部的成员 如下
  20.             int* pInt = &(mystr.x);
  21.             float* pFloat = &( mystr.y );
  22.             Console.WriteLine( *pInt);
  23.             Console.WriteLine( *pFloat );
  24.             //也可以用->来操作
  25.             int* pInt2 = &(pStruct -> x );
  26.             float* pFlost2 = &(pStruct -> y ) ;
  27.             Console.WriteLine( *pInt2 );
  28.             Console.WriteLine( *pFlost2 );
  29.             Console.Read();
  30.         }
  31.     }
  32.     unsafe struct Mystruct
  33.     {
  34.         public int x;
  35.         public float y;
  36.     }
  37. }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值