# Import
// Switch function void swap (int x, int y) {printf ("x = % d, y = % d", x, y); int temp = 0; temp = x; x = y; y = temp; printf ("x = % d, y = % d", x, y );} // void swap2 (int * x, int * y) {// do not change the address. Change the value of printf ("x = % d, y = % d ", * x, * y); int temp = 0; temp = * x; * x = * y; * y = temp; printf ("x = % d, y = % d ", * x, * y);} int main (int argc, const char * argv []) {// when defining pointers in C language, p is preferred, q, r, etc.; // The type is int * // variable name p // Initial Value NULL is always equal to 0 // int * p = NULL; /// print pointer % p // printf ("% p % lu \ n", p, sizeof (p )); /// char * q = 0*1000; // printf ("% p % lu \ n", q, sizeof (q )); # pragma mark --------- pointer summary // what we usually call a pointer refers to the pointer variable, // the pointer variable is the variable that stores the memory address (Memory number. // 32-bit, 8 0 or 1 indicates that each 8-bit one byte // The Memory occupied by the pointer is the same, and the memory occupied by the pointer variable is different // p and q occupy the same memory, because the first address in a 32-or 64-bit system accounts for so much, while int and char have different lengths. int occupies four rooms, char occupies 1 room // get the address character. It knows that int a = 3, a value is 3, int * P wants to point to a = 3 in memory, in this case, the first address of "& a" is "a". // int a = 3; // int * p = &; // printf ("p = % p \ n", p); // printf ("& a = % p \ n", & ); //// printf ("% d", * p); // char B = 'a'; // char * q = & B; // printf ("% p % lu \ n", q, sizeof (q); // The integer is equal to the integer address, the random type is equal to the random type address. // No one guesses the address. & the address operator is an integer pointer .... // * The content stored in the p value character address // The number of bytes depends on the type modifier // printf ("% d \ n", * p ); // * p value operator is used to obtain the value corresponding to the pointer // printf ("% d % c \ n", * p, * q ); // obtain the address in two ways // first, directly give the address // second, use the value symbol & to obtain # pragma mark ------------- assign a value to the pointer variable, indicates the pointer variable's point-to-point // pointer variable's point to // pointer variable's value assignment, indicates the pointer to the variable. // note // do not point the integer to the struct type. // do not point the struct to the integer. // p = & B; error // int a = 10; // int * p = & a; // printf ("the address where the pointer p is stored: % p the memory space occupied by the pointer p: % lu the value in the memory space: % d \ n ", p, sizeof (p), * p); // p also occupies memory. // they are all placed in the stack area, allocate memory one by one // printf ("p address: % p \ n", & p); // int B = 20; // p = & B; // printf ("new address saved by pointer p: % p value in the new memory space % d \ n", p, * p ); # pragma mark ------------- arithmetic operation of the pointer // only the addition and subtraction operations // int * p = & a; // P ++; Move Four bytes to the high bit // p --; move 4 bytes to the low position // p each plus one represents adding such a large type // p ++ // move four bytes to the high position // p + 1 // not changed // * (p ++) // * (p + 1) // int * p = & a; only tells the editor that p is a pointer. // * p = 20; operator, get the value corresponding to the address // p pointer (Address) * p value & p address & (* P) address # pragma mark ------------- array and pointer // int arr [5] = {9, 2, 3, 4, 5}; // printf ("% p \ n", arr ); // printf ("% p \ n", & arr [0]); // int * p = arr; // printf ("% d \ n ", * p); // printf ("% d \ n", * (p + 1); // printf ("--- % d \ n ", p [1]); // arr [1] = 8; // directly change the value // * (p + 1) = 7; // indirectly change the value // printf ("% d \ n", * (p + 1); # pragma mark ------------- add [] to the array by adding the * sign, expand the number // printf ("% d \ n", * (p + 1); // printf ("% d \ n", p [1]); // p [2] * (p + 2) arr [2] // p = arr + 3; // the pointer variable is a variable and can be repointed (assigned) // arr = p; the array name is a constant pointer and cannot be changed. // calculate the number of array elements through array elements, sizeof (a)/4 // short a [4] = {3, 7, 9, 1}; // int * p1 =; // char * p2 = a; // printf ("% d", * p1); // printf ("% d", * p2 ); # pragma mark ------------- string and pointer // you can operate a single character, * (p + 2) = 'Z'; // You can also operate the entire string printf ("% s ", (p + 2); // char str [] = "iPhone"; // char * p = str; // * (p + 2) = 'Z '; // printf ("% s", (p + 2); # pragma mark ------------- pointer array // The array storing pointers is the pointer array // constant zone // stack zone. copy the constant area // char * p1 = "iPhone"; // char * p2 = "Android"; // char * p3 = "win8 "; // char * strings [3] = {p1, p2, p3}; // char * strings [3] = {"iphone", "Android", "win8 "}; // operate the entire string // print the pointer array (pointer string) // for (int I = 0; I <3; I ++) {// printf ("% s", strings [I]); //} // printf ("% s, % s, % s", strings [0], strings [1], strings [2]); // operate a single string // * (strings [1] + 4) = 'X'; // point to a constant, cannot assign values/char c = * (strings [1] + 4); // printf ("\ n % c", * (strings [1] + 4 )); // printf ("\ n % c", strings [1] [4]); // # pragma mark ------------- the pointer and function // The value cannot be changed, because you didn't change a, B at all, but changed its copy x, y int a = 10, B = 20; // swap (a, B ); // printf ("% d, % d", a, B); // int * p = & a; // int * q = & B; // swap2 (p, q); // do not change the address. Change the value of swap2 (& a, & B); printf ("a = % d, B = % d ", a, B); // to learn the pointer well, You need to draw more and return 0 ;}