[C#] C# 创建和访问指针数组的代码片段 →→→→→进入此内容的聊天室

来自 , 2019-03-31, 写在 C#, 查看 137 次.
URL http://www.code666.cn/view/146f7dd4
  1. class UnsafePointerArray
  2. {
  3.    public struct AStruct
  4.    {
  5.       public int anInteger;
  6.    }
  7.  
  8.    public static void CreatePointerArray()
  9.    {
  10.       AStruct struct0 = new AStruct();
  11.       AStruct struct1 = new AStruct();
  12.       AStruct struct2 = new AStruct();
  13.            
  14.       unsafe
  15.       {
  16.          AStruct*[] StructPtrs = new AStruct*[3];
  17.  
  18.          // load addresses into pointer array
  19.          StructPtrs[0] = &struct0;
  20.          StructPtrs[1] = &struct1;
  21.          StructPtrs[2] = &struct2;
  22.  
  23.          fixed (AStruct** ptrArrayStructPtrs = StructPtrs)
  24.          {
  25.             for (int i = 0; i < 3; i++)
  26.             {
  27.                ptrArrayStructPtrs[i]->anInteger = i * 2;
  28.                Console.WriteLine ("&struct" + i + " = "  
  29.                   + String.Format ("{0:x2}", (int) ptrArrayStructPtrs[i]));
  30.                Console.WriteLine ("&struct" + i + ".anInteger = "
  31.                   + ptrArrayStructPtrs[i]->anInteger + "\n\n");
  32.             }
  33.          }
  34.       }
  35.    }
  36. }
  37.  
  38.  
  39. //csharp/4153

回复 "C# 创建和访问指针数组的代码片段"

这儿你可以回复上面这条便签

captcha