- class UnsafePointerArray
- {
- public struct AStruct
- {
- public int anInteger;
- }
- public static void CreatePointerArray()
- {
- AStruct struct0 = new AStruct();
- AStruct struct1 = new AStruct();
- AStruct struct2 = new AStruct();
- unsafe
- {
- AStruct*[] StructPtrs = new AStruct*[3];
- // load addresses into pointer array
- StructPtrs[0] = &struct0;
- StructPtrs[1] = &struct1;
- StructPtrs[2] = &struct2;
- fixed (AStruct** ptrArrayStructPtrs = StructPtrs)
- {
- for (int i = 0; i < 3; i++)
- {
- ptrArrayStructPtrs[i]->anInteger = i * 2;
- Console.WriteLine ("&struct" + i + " = "
- + String.Format ("{0:x2}", (int) ptrArrayStructPtrs[i]));
- Console.WriteLine ("&struct" + i + ".anInteger = "
- + ptrArrayStructPtrs[i]->anInteger + "\\n\\n");
- }
- }
- }
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/050620133797.html
来源: http://www.codesnippet.cn/detail/050620133797.html