在c#中使用指针

时间:2023-12-24 19:00:19

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

在c#中使用指针

然后将有关指针,地址的操作放在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.

}

示例:

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

输出结果:

在c#中使用指针

关键字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. 请尊重原作者版权