Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and beauty - How to assign a value to a pointer
How to assign a value to a pointer
1. Open visual studio and create a console application to demonstrate the use, assignment and value taking of pointers.

2. In the Main method, add an unsafe code segment, and all pointers in C# syntax need to be placed in this code segment.

3. The only difference between defining a pointer variable and defining an ordinary variable is that an asterisk is added after the type of the variable. For example, an integer pointer variable p is defined in the following form:

int * p;

4. If you directly assign the value of 10 like a normal variable, it can be seen from the code effect that this is not allowed, and the compiler directly reports an error.

5. Then, define another public variable, such as int x = 100.

6. Use&; The symbol in front of the variable indicates the memory address of the variable. At this time, it can be copied to the pointer. For example, assign the address of the integer variable x to the pointer p:

P =&.

7. Put an asterisk in front of the pointer variable to indicate that the value in the address pointed by this pointer has been obtained. For example, if you want to output the value of pointer p, use * p.