Daily notes (pointer) for IOS learning, daily ios Learning

Source: Internet
Author: User

Daily notes (pointer) for IOS learning, daily ios Learning

Knowledge Point sorting for IOS Learning (C language)

I. pointer

1) concept: a variable that stores the address of a variable.

2) Data Storage Type Analysis

1. text (code segment): stores binary executable code

2. data (initialized data Segment) Stores initialized global variables and initialized static modified variables, which are divided into read-only data segments.

(Such as string constants and Integer constants), readable and writable data segments, and static storage.

3. data (uninitialized data segment) Stores uninitialized global variables and uninitialized static modified variables for static storage.

4. The heap (heap) memory needs to be manually applied for and manually released. The space is very large. It usually stores large amounts of data and has low execution efficiency.

It is troublesome to use and dynamic storage.

5. the memory space of the stack (stack area) is relatively small, and the local variable development of function calls is on the stack, which is very efficient.

3) The "&", indirect addressing character "*", and "% p" print the format characters of the address.

4) pointer identifier: * For example: int * p indicates that a pointer p is defined, * Indicates a pointer, int indicates the type pointed to by p (where the address of the int type variable is stored)

5) All types of pointers share the same space in the memory. The 64-bit system contains 8 bytes (32-bit is 4 bytes ).

6) pointer operation pointer object ++, indicating to point to address backward offset, offset is sizeof (pointing to type);--indicates to forward offset.

7) Wild pointer: A pointer with uninitialized values, which contains a random address. You can modify the value corresponding to this address at will, which will cause uncontrollable impact to the system.

8) NULL pointer p = NULL = 0; 0 is an Invalid Address. 0 cannot be assigned a value and cannot be read or written.

9) Advantages of pointers

1. provides flexible methods for modifying call variables for Functions

2. Multiple return values can be returned for a function.

3. It can improve the efficiency of some subprograms. When transferring data, if the data block is large (such as the data buffer area or a large structure), you can use the pointer transmission address.

Not the actual data, it can improve transmission speed and save a lot of memory.

4. Provides support for dynamic data structures (such as binary trees and linked lists.

10) void * pointer, with a wildcard pointer, but pointing to any type.

11) pointer application. The pointer in the function serves as the input parameter of the function.

12) int * a; pointer variable, pointing to int type data.

13) pointer array refers to a pointer of the array type.

For example: int * p [4]: indicates four pointers pointing to the int type. Each pointer is independent and does not matter. p [I] is an int type pointer.

P [I] = * (p + I)

Instance code:

1 int add (int * a [], int len1, int len2) 2 {3 int sum = 0; 4 for (int I = 0; I <len1; I ++) {5 // a [I] The I pointer = the I-th 1D array 6 for (int j = 0; j <len2; j ++) {7 sum = sum + * (a [I] + j); 8} 9} 10 return sum; 11} 12 13 int main () 14 {16 int a [4] [3] = {1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10, 11, 12 }}; 17 int * p [4]; // pointer array, which cannot be used without initialization. The wild pointer group is 18 for (int I = 0; I <4; I ++) {19 p [I] = a [I]; // a [I] indicates an array 20} 22 int sum = add (p, 4, 3) of the I-th row ); 23 printf ("sum = % d \ n", sum); 24 return 0; 25}

14) array pointer

Instance code:

1 int add (int (* p) [3], int len) 2 {3 int sum = 0; 4 for (int I = 0; I <len; I ++) {5 // p + I indicates pointing to the I-th array * (p + I) = a [I] 6 for (int j = 0; j <3; j ++) {7 // a [I] [j] = * (a [I] + j) = * (p + I) + j) 8 sum = sum + * (p + I) + j); 9} 10} 11 12 return sum; 13} 14 15 int main () 16 {17 int a [4] [3] = {1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10, 11, 12 }}; 19 // a [0], a [1], a [2], a [3]: each of the four arrays has three elements, 21 int (* p) [3]; // you can point to an array with three elements 23 // p = & (a [0]); // & a [0] = & (* (a + 0) 25 p = a; // simplify the formula 27 int sum = add (, 4); // Since p = a, you can write 29 printf ("sum % d \ n", sum); 31}

15) function pointer int (* pfunc) (int, int); defines a function pointer, which must point to a function of the int XXX (int a, int B) style.

Instance code:

1 int add (int a, int B) 2 {3 return a + B; 4} 5 6 int main () {7 int a = 10; 8 int B = 20; 9 int sum; 10 int (* pfunc) (int, int); 11 12 pfunc = add; // The function name is equivalent to a constant 14 sum = pfunc (a, B) pointing to its function entry pointer; 16 printf ("sum = % d \ n", sum ); 18 return 0; 20}

16) Implement function callback using pointers

Implementation Code:

1 void print_hello () 2 {3 printf ("hello world \ n"); 4} 5 6 void print_date () 7 {8 time_t rawtime; 10 struct tm * timeinfo; 12 time (& rawtime); 14 // to obtain the system's local time, reference the time header file, # include <time. h> 16 timeinfo = localtime (& rawtime); 18 printf ("% 4d-% 02d-% 02d % 02d: % 02d: % 02d \ n ", 1900 + (* timeinfo ). tm_year, 1 + (* timeinfo ). tm_mon, 20 (* timeinfo ). tm_mday, (* timeinfo ). tm_hour, (* timeinfo ). tm_min, (* timeinfo ). tm_sec); 22} 23 24 // void (* pfunc) () 25 void print_all (void (* pfunc) (), int count) 26 {27 for (int I = 0; I <count; I ++) {28 pfunc (); 29} 31} 33 34 int main () 35 {37 print_all (print_date, 4); // loop output for 4 times 39 print_all (print_hello, 1); 41 return 0; 42}

17) Implement recursive algorithms using pointers

Implementation Code:

Void sort_array (int * a, int len) {// bubble sort int temp; for (int I = 0; I <len-1; I ++) {for (int j = 0; j <len-1-i; j ++) {if (* (a + j)> * (a + j + 1 )) {temp = * (a + j); * (a + j) = * (a + j + 1); * (a + j + 1) = temp ;}}}}
Int main () {int a [5] = {, 5}; int * p; p = a; // pass the array: two forms of array or pointer print_array (a, 5); return 0 ;}

 

18) the second-level pointer points to the pointer.

Instance code:

1 void swap (int ** p, int ** q) 2 {4 int temp; 6 temp = ** p; 8 ** p = ** q; 10 ** q = temp; 12} 13 15 int main () 16 {18 int a = 10; 20 int B = 20; 22 int * p = &; // p is a pointer pointing to the int type 24 int ** q; // q is a pointer pointing to the int type *, and the int type * indicates the int type, p is the type 26 q = & p; // * q = * (& p) = p28 printf ("% d \ n", ** q ); // * q = p, ** q = * p = a30 int * p_ B = & B; 32 int ** pp_ B = & p_ B; 34 swap (q, pp_ B ); 36 printf ("a = % d, B = % d \ n", a, B); 38 return 0; 39}

19) const modifier, read-only representation: name of the const data type variable. The variable directly modified by const cannot be changed, but the variable added with * can be modified.

Instance code:

1 int main () 2 {4 int value1 = 10; 6 int value2 = 20; 8 const int a = 100; 10 printf ("a = % d \ n ", a); 12 a = 20; // The variable modified by const cannot be changed to 14 const int * p = & value1; 16 p = & value2; 18 // * p = 100; // * p cannot be modified. p can be changed to 20 int const * m = & value1; 22 m = & value2; 24 // * m = 10; // * m cannot be modified. m can be changed to 26 int * const n. // n cannot be changed, * n can be changed to 28 const int * const q // q cannot be changed, * q cannot be changed to 30 const int * const m, * n; // m, * m cannot be changed, n can be changed to 32 * n = 100; // * n cannot be changed to 34 const int * const m, * const n; // m, * m, n cannot be changed to 36 * n = 100; // * n cannot be changed to 38 int * const m, * const n; // m, n cannot be changed, * m, * n can be changed, 40 * n = 100; 41}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.