- using System;
- using System.Collections.Generic;
- using System.Text;
- namespace 不安全代码
- {
- unsafe class 指针访问结构
- {
- public static void Main()
- {
- Mystruct* pStruct;
- Mystruct mystr = new Mystruct();
- //指针指向结构
- pStruct = &mystr;
- ( *pStruct ).x = 500;
- ( *pStruct ).y = 12.0f;
- // *pStruct ).x写法过于复杂,c#提供了指针预算符 ->
- Console.WriteLine( pStruct ->x );
- Console.WriteLine( pStruct->y );
- //也可以把指针直接指向结构内部的成员 如下
- int* pInt = &(mystr.x);
- float* pFloat = &( mystr.y );
- Console.WriteLine( *pInt);
- Console.WriteLine( *pFloat );
- //也可以用->来操作
- int* pInt2 = &(pStruct -> x );
- float* pFlost2 = &(pStruct -> y ) ;
- Console.WriteLine( *pInt2 );
- Console.WriteLine( *pFlost2 );
- Console.Read();
- }
- }
- unsafe struct Mystruct
- {
- public int x;
- public float y;
- }
- }
c#中指针操作结构
最新推荐文章于 2022-01-09 14:44:53 发布