在c#中使用指针

   如果想在c#中使用指针,首先对项目进行配置:在解决方案资源管理器中右击项目名选择属性(或在项目菜单中选择consoleApplication属性(consoleApplication为项名)),在生成选项卡中 选中“允许不安全代码”,如下图:

      然后将有关指针,地址的操作放在unsafe语句块中。使用unsafe关键字是来告诉编译器下面的代码是不安全的。

unsafe关键字的使用:

 (1)放在函数前,修饰函数,说明在函数内部或函数的形参涉及到指针操作

       unsafe static void FastCopy(byte[] src, byte[] dst, int count)

{

    // Unsafe context: can use pointers here.

}

      不安全上下文的范围从参数列表扩展到方法的结尾,因此指针作为函数的参数时须使用unsafe关键字

         unsafe static void FastCopy ( byte* ps, byte* pd, int count ) {...}

(2)将有关指针的操作放在由unsafe声明的不安全块中

       unsafe

      {   

         // Unsafe context: can use pointers here. 

      }

 

示例:

[csharp] view plain copy
 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace ConsoleApplication1  
  7. {    
  8.     class Program  
  9.     {  
  10.   
  11.           
  12.         static void Main(string[] args)  
  13.         {  
  14.   
  15.             int i = 1;  
  16.             unsafe   
  17.             {   
  18.                 Increment(&i); //将函数调用放在unsafe中是因为实参是指针类型  
  19.             }  
  20.               
  21.             Console.WriteLine(i+"\n");  
  22.   
  23.             //演示如何使用指针来操作字符串  
  24.             string s = "Code Project is cool";  
  25.             Console.Write("the original string : ");  
  26.             Console.WriteLine("{0}\n", s);  
  27.   
  28.             char[] b = new char[100];  
  29.             s.CopyTo(0, b, 0, 20);//public void CopyTo (int sourceIndex,char[] destination,int destinationIndex,int count)  
  30.             //将指定数目的字符从此实例中的指定位置复制到 Unicode 字符数组中的指定位置。  
  31.   
  32.             Console.Write("the encoded string : ");  
  33.   
  34.             unsafe   
  35.             {  
  36.                 fixed (char* p = b)   
  37.                 {   
  38.                     NEncodeDecode(p);   
  39.                 }  
  40.             }  
  41.             for (int t = 0; t < 20; t++)  
  42.                 Console.Write(b[t]);  
  43.             Console.WriteLine("\n");  
  44.   
  45.             Console.Write("the decoded string : ");  
  46.             unsafe  
  47.             {   
  48.                 fixed (char* p = b)   
  49.                 {   
  50.                     NEncodeDecode(p);   
  51.                 }   
  52.             }  
  53.             for (int t = 0; t < 20; t++)  
  54.                 Console.Write(b[t]);  
  55.             Console.WriteLine();  
  56.   
  57.         }  
  58.   
  59.   
  60.         unsafe public static void Increment(int* p)  
  61.         {  
  62.   
  63.             *p = *p + 1;  
  64.         }  
  65.   
  66.         unsafe public static void NEncodeDecode(char* s)  
  67.         {//将一段字符串通过异或运算进行编码解码的操作。如果您将一段字符串送入这个函数这个字符串将会被编码,  
  68.         //如果您将一段已经编码的字符送入这个函数,这段字符串将会被解码。  
  69.             int w;  
  70.             for (int y = 0; y < 20; y++)  
  71.             {  
  72.                 w = (int)*(s + y);  
  73.                 w = w ^ 5;//^为按位异或。  
  74.                 *(s + y) = (char)w;  
  75.             }  
  76.   
  77.         }  
  78.     }  
  79. }  


输出结果:

 

关键字fixed简介:

   fixed语句只能出现在不安全的上下文中。

   C# 编译器只允许在 fixed 语句中分配指向托管变量的指针,无法修改在 fixed 语句中初始化的指针

    fixed 语句禁止垃圾回收器重定位可移动的变量。 当你在语句或函数之前使用fixed时,你是在告诉.Net平台的垃圾回收器,在这个语句或函数执行完毕前,不得回收其所占的内存空间。

 示例:

(1)

// assume class Point { public int x, y; }  pt is a managed variable, subject to garbage collection.

Point pt = new Point();

// Using fixed allows the address of pt members to be taken, and "pins" pt so it isn't relocated.(pin  v 固定住)

fixed ( int* p = &pt.x )

{

    *p = 1;

}

(2)用数组或字符串的地址初始化指针:

          fixed (int* p = arr) ...  // equivalent to p = &arr[0]

          fixed (char* p = str) ... // equivalent to p = &str[0]

(3)只要指针的类型相同,就可以初始化多个指针:

        fixed (byte* ps = srcarray,pd = dstarray) {...}

      要初始化不同类型的指针,只需嵌套 fixed 语句:

                fixed (int* p1 = &p.x)

   {

      fixed (double* p2 = &array[5])

    {       

      // Do something with p1 and p2.

   }

  }

 

 

 

 

原文转自:http://blog.csdn.net/susan19890313/article/details/7365996

原作者为 susan19890313. 请尊重原作者版权

 

posted @ 2016-10-11 13:33  LonelyEnvoy  阅读(788)  评论(0编辑  收藏  举报