Pointer (1), pointer (
Speaking of C language, we have to raise pointers. How many heroes are planted in the truth. Arrays and strings can be easily used using the finger program (the String type in C # is the reference type, that is, the pointer type ). The pointer supports dynamic memory allocation, which improves the efficiency of the program. "Artificial guidance is required for precision strikes." --- missile soldiers often say this in Military travel dramas. Pointers are similar to coordinates of Manual guidance. It is flexible, easy to use, and complicated at the same time. Guiding errors is a devastating blow. For example, there are 10 rooms on the first floor of the Mobile Building, numbered 1-10 in sequence. Company A is rented from company 2, and company A is rented from Company 5-8. The first address of each room is the company address. If we want to send A courier to Company A, it is to send the courier to the company whose address is 2. If you send a courier to Company B, the courier is sent to the company with the address of 5. Similarly, in the programming process, the system sets a number for each byte in the memory to manage the memory. All variables must defeat one or several consecutive bytes in the memory. The first byte is the address of the variable. If there is an INT type A, the system will allocate two bytes of storage units for A during compilation. Assume that the numbers of these two bytes are 001 and 002 respectively. The address of A over there is 001;
Type * variable name; --- define pointer
& The address operator followed by a variable to read the storage address of the variable;
*: The variable operator. Used to read the variable value of the pointer;
//// Main. m // ZhiZhen /// Created by Cheng Yingying on. // Copyright, 2017, Cheng yingmiao. all rights reserved. // # import <Foundation/Foundation. h> int main (int argc, const char * argv []) {@ autoreleasepool {int a = 10; // variable int * B; // pointer type B = & a; // address A to B, it can also be said that B Represents the address NSLog of A (@ "print the value in the address pointed by pointer B: % d", * B ); // print the value * B = 20 pointing to a in the pointer; // assign a value to the pointer, that is, assign an NSLog to the variable a through B (@ "the value of a has changed, its value is: % d ", a); // The value of a has changed} // @ autoreOVER return 0 ;}-------------------- Result ------------------ 22:20:29. 835850 ZhiZhen [8931: 355806] print the value in the address pointed to by pointer B: 102017-03-21 22:20:29. 836091 ZhiZhen [8931: 355806] the value of a has changed and its value is 20.