---
title: "[老文章转载]C#中使用指针"
author: "ta8210"
date: "2007-04-27T20:16:00Z"
tags: [".net", "C", "C#", "C++", "J#"]
slug: "c"
draft: false
lastmod: "2007-04-27T20:16:00Z"
id: 574314
url: "https://ta8210.iteye.com/blog/574314"
---

我想许多C程序员不愿意学习Java的原因就是Java不支持指针，但是现在类似于Java的C#却已经支持了指针，你可以使用unsafe关键字来告诉编译器下面的函数或者代码是不安全的。一旦你使用了unsafe，那么你就可以在unsafe区域中使用指针。

程序1

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)usingSystem;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)classnish  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)unsafestaticvoidIncrement(int\*p)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//incrementtheintpointedtobyp  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*p=\*p+1;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)publicstaticvoidMain()  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)inti=1;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//wepasstheaddressoftheinttothefunctionasitexpectsapointer  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)unsafeIncrement(&i);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//nowweprintoutthevalueofi  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)Console.WriteLine(i);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}

当你运行这个程序，你将会看到输出结果2。这是因为你已经将变量i的地址送给了函数Increment来处理。变量i建立于栈中，&i则表示它在栈中的地址。这样在函数Increment中，p就指向i的地址，当我们对\*p加1时，实际上是对变量i进行增加。

程序2  
下面的程序将会让你更清楚的了解：

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)usingSystem;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)classnish  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)unsafepublicstaticintMain()  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)intj=100;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)intk=100;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)Console.WriteLine("addressofj={0}andaddressofk={1}",(int)&j,(int)&k);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)Console.WriteLine("j={0}k={1}",j,k);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)int\*p;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)p=&j;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)Console.WriteLine("pnowpointsto{0}",(int)p);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*p=200;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)Console.WriteLine("j={0}k={1}",j,k);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)p=&k;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)Console.WriteLine("pnowpointsto{0}",(int)p);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*p=300;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)Console.WriteLine("j={0}k={1}",j,k);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)return0;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

当运行上面的程序，我们将得到下面的结果。你会看到一些你熟悉的东东，下面的输出结果将会清楚的看到程序执行的过程：

address of j=1244312 and address of k=1244308  
j=100 k=100  
p now points to 1244312  
j=200 k=100  
p now points to 1244308  
j=200 k=300

首先将变量的j的地址赋给p，这样当我们改变\*p时，j的值也将自动改变。接着我们将p指向变量k的地址，这时改变\*p则是在改变k。

同样需要清楚的就是变量p也有自己的地址，下面的程序将会清楚的告诉您一切。

程序 3

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)usingSystem;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)classnish  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)publicstaticvoidMain()  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)unsafe  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)inta=100;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)int\*p;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)p=&a;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)Console.WriteLine("addressofais{0}",(int)&a);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)Console.WriteLine("pnowpointsto{0}",(int)p);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)Console.WriteLine("addressofthepointervariablepis{0}",(int)&p);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

一运行上面的代码，我们将获得下面显示的输出。你将同样获得一些类似的输出，注意这里unsafe关键字的使用。

address of a is 1244312  
p now points to 1244312  
address of the pointer variable p is 1244308

1244308是指针变量p的地址，而1244312则是指针p所指向的地址，我们使用\*p来获得。

程序 4  
Okey。在最后的一个程序中，我将向大家介绍如何使用指针来操作字符串。在这个程序中存在一个程序来将一段字符串通过异或运算进行编码解码的操作。如果您将一段字符串送入这个函数这个字符串将会被编码，如果您将一段已经编码的字符送入这个函数，这段字符串将会被解码。当这并不是一个安全的加密方法，我仅仅是想通过这个例子来演示一下指针的作用。

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)usingSystem;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)classnish  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)publicstaticvoidMain()  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)strings="CodeProjectiscool";  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)Console.Write("theoriginalstring:");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)Console.WriteLine("{0} ",s);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)char\[]b=newchar\[100];  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)s.CopyTo(0,b,0,20);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)Console.Write("theencodedstring:");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)unsafefixed(char\*p=b)NEncodeDecode(p);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)for(intt=0;t&lt;20;t++)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)Console.Write(b\[t]);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)Console.WriteLine(" ");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)Console.Write("thedecodedstring:");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)unsafefixed(char\*p=b)NEncodeDecode(p);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)for(intt=0;t&lt;20;t++)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)Console.Write(b\[t]);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)Console.WriteLine();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)unsafepublicstaticvoidNEncodeDecode(char\*s)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)intw;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)for(inty=0;y&lt;20;y++)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)w=(int)\*(s+y);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)w=w^5;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*(s+y)=(char)w;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)

下面是我得到的输出，你也同样可以得到：

the original string : Code Project is cool

the encoded string : Fja\`%Uwjo\`fq%lv%fjji

the decoded string : Code Project is cool

本例中，你会发现一个新的关键字--fixed。当你在语句或函数之前使用fixed时，你是在告诉.Net平台的垃圾回收器，在这个语句或函数执行完毕前，不得回收其所占的内存空间。fixed关键字只能在不安全的代码中使用。本例中如果不使用fixed关键字，那么这段程序的执行的结果是不可预知的，因为垃圾回收器会不断的回收这些可控制代码的所占用的内存空间。幸运的是，编译器不会允许您指向可控制变量，除非您使用了fixed关键字。

在这个函数中，你看到我使用了\*(s+y)表达式。s是指向字符串的地址（注意是首地址）。y则通过循环从0增加到19。这样\*(s)给我们的地址是1000,\*(s+1)则是1002，\*(s+2)则是1004，以此类推。编译器知道我指向的是一个字符数据，因此第次就向前移动两个字节，因为char是16位的（这与c是不同的，c中char只占8位）。

结论  
使用不安全代码时，必须十分小心，任何细小的错误，甚至是类型错误，都会导致输出结果的不可遇知，而且这种错误是很难调试发现。当然如果您是C/C++程序员，您已经对指针了解得十分透彻，那么当我的话没有说。呵呵，祝好运。