程序1 using System; class nish { unsafe static void Increment(int* p) { //increment the int pointed to by p *p=*p+1; } public static void Main() { int i = 1; //we pass the address of the int to the function as it expects a pointer unsafe Increment(&i); //now we print out the value of i Console.WriteLine (i); } }
using System; class nish { unsafe public static int Main() { int j=100; int k=100; Console.WriteLine("address of j={0} and address of k={1}",(int)&j,(int)&k); Console.WriteLine("j={0} k={1}",j,k); int *p; p=&j; Console.WriteLine("p now points to {0}",(int)p); *p=200; Console.WriteLine("j={0} k={1}",j,k); p=&k; Console.WriteLine("p now points to {0}",(int)p); *p=300; Console.WriteLine("j={0} k={1}",j,k);
程序 3 using System; class nish { public static void Main() { unsafe { int a=100; int *p; p=&a; Console.WriteLine("address of a is {0}",(int)&a); Console.WriteLine("p now points to {0}",(int)p); Console.WriteLine("address of the pointer variable p is {0}",(int)&p); } } }
using System; class nish { public static void Main() { string s="Code Project is cool"; Console.Write("the original string : "); Console.WriteLine("{0}\r\n",s);