The basic use of C + + pointers requires specifying unsafe compilation options.
usingSystem;usingSystem.IO;usingSystem.Reflection;usingSystem.Runtime.Serialization.Formatters.Binary;namespaceconsoleapplication1{Internal classProgram {unsafe Private Static voidMain (string[] args) { intx =Ten; Shorty =-1; bytey2 =4; Doublez =1.5; int* p = &x; Short* py = &y; Double* PZ = &Z; Console.WriteLine ("x {0},{1},{2}", (UINT) &x,sizeof(int), x); Console.WriteLine ("y {0},{1},{2}", (UINT) &y,sizeof( Short), y); Console.WriteLine ("y2 {0},{1},{2}", (UINT) &y2,sizeof(byte), y2); Console.WriteLine ("z {0},{1},{2}", (UINT) &z,sizeof(Double), z); Console.WriteLine ("p {0},{1},{2}", (UINT) &p,sizeof(int*), (UINT) (p); Console.WriteLine ("py {0},{1},{2}", (UINT) &py,sizeof( Short*), (UINT) py); Console.WriteLine ("PZ {0},{1},{2}", (UINT) &pz,sizeof(Double*), (UINT) PZ); *p = -; Console.WriteLine ("x {0}", x); Console.WriteLine ("p {0}", *p); //after casting the result is not sure that this and unsafe have nothing to do with C + + pointers This cannot be donePZ = (Double*) p; Console.WriteLine ("PZ {0}",*PZ); } }}
The struct is an unmanaged type, the class is a managed type, and the address of the class is changed by the operation of the GC and requires the use of fixed.
usingSystem;usingSystem.IO;usingSystem.Reflection;usingSystem.Runtime.Serialization.Formatters.Binary;namespaceconsoleapplication1{Internal classProgram {Internal structcurrencystruct { Public Longdollars; Public bytecents; Public Override stringToString () {return string. Format ("Doolars: {0}, Cents: {1}", dollars, cents); } } Internal classCurrenyclass { Public Longdollars; Public bytecents; Public Override stringToString () {return string. Format ("Dollars: {0}, Cents: {1}", dollars, cents); } } unsafe Private Static voidMain (string[] args) {Console.WriteLine (sizeof(currencystruct)); Currencystruct Amount1, Amount2; Currencystruct* Pamount = &Amount1; Long* Pdollars = & (pamount->dollars); byte* pcents = & (pamount->cents); Console.WriteLine ("{0}", (UINT) &amount1); Console.WriteLine ("{0}", (UINT) &Amount2); Console.WriteLine ("{0}", (UINT) &pamount); Console.WriteLine ("{0}", (UINT) &pdollars); Console.WriteLine ("{0}", (UINT) &pcents); Pamount->dollars = -; *pcents = -; Console.WriteLine (AMOUNT1); //point to Amount2pamount--; Console.WriteLine ("{0}, {1}", (UINT) Pamount, *pamount); //pcents pointing to Amount2currencystruct* ptempcurrency = (currencystruct*) pcents; Pcents= (byte*) (--ptempcurrency); Console.WriteLine ("{0}", (UINT) &pcents); Currenyclass Amount3=NewCurrenyclass (); fixed(Long* PDollar3 = &(amount3.dollars))fixed(byte* PCents3 = &(amount3.cents)) {Console.WriteLine ("{0}", (UINT) PDOLLAR3); Console.WriteLine ("{0}", (UINT) pCents3); *PDOLLAR3 =- -; Console.WriteLine ("{0}", Amount3); } } }}
Create high-performance, low-overhead arrays in the stack
usingSystem;usingSystem.IO;usingSystem.Reflection;usingSystem.Runtime.Serialization.Formatters.Binary;namespaceconsoleapplication1{Internal classProgram {Private Const intLen = +; unsafe Private Static voidMain (string[] args) { Long* Parray =stackalloc Long[Len]; for(inti =0; i < Len; i++) {Parray[i]= i *i; } for(inti =0; i < Len; i++) {Console.WriteLine ("{0}", Parray[i]); } } }}
C # uses C + + pointers