This series of articles is some very simple learning methods and technologies.
It can make you feel like you want to make you worship, this is the pointer - the invisible magic wand in the program - Skyala
Pointer advantages: 1. Provide a means for modifying calls to the function; 2. Supports C dynamic distribution subroutine 3. The efficiency of certain subroutines can be improved 4. Support for dynamic data structures (such as binary tree, linked list): pointers introduced a layer of indirectibility for programs, can manipulate pointers without directly manipulating objects. 1. The controlled pointer includes an address of the pointer to the object 2 referred to. When the pointer does not point to any object, when writing * pi, the program may make the program in the execution period, such as addressed to an object, then the operation, does not point to any object, will be wrong, so determine before the promotion It does point to an object. A pointer not pointing to any object, an internal address is 0, sometimes referred to as a NULL pointer, assert (p! = 0) detects whether to assign success. You can also use IF (Pi), only when PI is a non-zero value, is True. 1. Definition: To store the memory address variable. Interpretation: The pointer also has its own address for a data type. Taking the four bytes of storage space INT * P: & P returns the address of the pointer P, not the address address of the variable, generally refers to the position of the other variable in the memory, pointer variable: Type * Name declares Make sure that its type is compatible with the object type you want to point to the "closest" principle pointing to integer constants: const INT * P; It points to the value that only reads * p = 4 (error), p = 5 (correct) point to a constant pointer: int * const page, does not allow the value of the pointer variable, * p = 5 (correct), P = 5 (error) three, pointer operators: & (address operator : One yuan operator, only acts on an operand, return the address of the operand * (Promotion operation): One yuan operator, is the supplement operation, returning to the value of the value indicated by its operand, pointer assignment and conversion : That type is directly assigned, the heterogeneous type is converted. Forced conversion: You can turn the expression results to the specified type char * p; (int *) P to convert the P to INT type, remember to pay attention to the size of the two types during the conversion, and there may be data Lost (such as int to double) involving void *: c, Void * type can assign any type of pointer, and vice versa of C needs to force conversion VOID * can be used as infinity can accept any type assignment, but not int * P = 9; void * t = p (correct); P = T (error) does not involve VOID * to force convert five, pointer arithmetic operation and integer addition, subtraction, own increment, reduction point increase After the amount, the next element is the same as the pointer base class, and the increase or decrease unit is the length of the type fingered. Sixth, other instructions: 1. Pointers and arrays: Do not bring the subscript of the array name to return the start address of the array, ie the address of the array header, so there are two ways to access the array: array subscript and pointer arithmetic 2. Function pointer: The function has a physical memory address that can be assigned to the pointer, and a function address is also the entry point of the function, but also the address of the call function 3. Multi-level pointer address ** P Seven, dynamic memory allocation definition: It is a method that acquires memory in operation. Is a free memory area from a heap-system-acquired internal departure operator: New (malloc in C): Automatically establish an object with a suitable size, returns a pointer with the correct type, such as allocation is unsuccessful, return one Empty pointer 0 and can automatically call the constructor.
Char * p = new char ('t'); DELETE (Free): delect P; Free Brand DELETE [] P; 8, with reference to the reference & reference operator: 1 when the array object is released; Quote is just the alias of the variable, not a pointer to the variable (distinguishes that the address operator "&") does not account for memory space, and changes to variables will also change. 2. You cannot use the pointer indirect operator "*" to use the pointer 3. The reference must initialize int & c = count; (c is the alias of count) 9. Note: Be initialized before each pointer is used. To prevent a pointer from the empty object. Application example (POINTER.CPP)
Compilation environment: window2000 vc6.0 #include
/ / The reference variable is initialized // int & t; (error) // t = count; // can not be used with a pointer indirect arithmetic repetition, the reference is just a variable alias // it does not occupy address space / / cout << * c << endl; int * cis = 0; // pointing to integer constants pointers, pointer is best performed initialization int * it = & if; // * cis = 100; Error), * CIS is constant cis = it; cout << CIS << "<< * cis << endl; int * const ICS = & if; // pointing to constant pointers * ICS = 10; ICS = IT; COUT << ICS << "<< * ICS << Endl;} Procedure - Procedures for Ideas Never touch people - Skyala