Using system; using system. collections. generic; using system. text; namespace helloworld {class program {static void main (string [] ARGs) {// 1. pointer definition // 1.1 pointer is the data type that specifically processes the address // 1.2 pointer is the first address of a memory unit // 2.c# pointer and C, the difference between C ++ (here we need to know the two keywords unsafe and fixed) // 2.1 pointers are not recommended in C, pointer-related operations are considered unsafe ). Therefore, before running this code, you must change it to another place. Otherwise, the Code cannot be compiled. // Modification method: // find your project in Solution Explorer on the right, right-click the project icon (green), and select the last properties, then select allow unsafe code on the build tab. Then this code can run. You will see that the above Code can manipulate the array with pointers like the C language. The premise is that there must be fixed (int * P = array), which means that P is fixed to the array and cannot be modified. Because the automatic garbage collection mechanism of C # will allow the allocated inner to adjust the position during running, if so, P may start with array, however, after the location of array is adjusted to another location, P points to no array. Therefore, you need to add a fixed keyword and place it there, so that subsequent operations will be guaranteed. // You can directly view the code below: * ******************* // 1.1 int type pointer (value type) // C # when operating the pointer, it must be placed in the Unsafe code block unsafe {// declare an int variable value of 10; int I = 10; // declare a pointer variable int * INTP; // point the pointer to the variable I intp = & I; console. writeline ("variable I value: {0}", I); console. writeline ("memory address of variable I: {0: x}", (INT) INTP); console. writeline ("the memory address of the pointer INTP is: {0: x}", (INT) & INTP); console. writeline ("pointer INTP value: {0}", * INTP);} // 1.2 string type (reference type) unsafe {// declare Strings are Hello world! (String type is an array of char) string _ STR = "Hello world! "; // As mentioned above, for GC reasons in C #, the function of adjusting the memory in the hosted spine to the position of fixed is to fix it. fixed (char * STRP = _ Str) {console. writeline ("Variable _ STR value: {0}", _ Str); console. writeline ("Variable _ STR address: {0}", (INT) STRP); console. writeline ("the STRP value of the pointer is {0}", * STRP); // an error is prompted. The address of the read-only local variable cannot be obtained because STRP is read-only. the compiler does not allow STRP to change the pointer. therefore, compilation fails. // console. writeline ("STRP pointer address: {0}", (INT) & STRP); // loop output string char * CHP = STRP; For (INT I = 0; CHP [I]! = '\ 0'; // The end character of the string is' \ 0' I ++) {console. write (CHP [I]);} console. writeline (); console. writeline ("STRP pointer address: {0: x}", (INT) CHP); console. writeline ("CHP pointer address: {0: x}", (INT) & CHP );}} // 1.3 array unsafe {// declare an array int [] arrint = {1, 2, 3, 4, 5, 6, 7, 87, 8 }; fixed (int * IP = arrint) {// cyclic output array int * P = IP; For (INT I = 0; I <arrint. length; I ++) {console. write (P [I] + "") ;}} console. readkey ();}}}
Output result: