C language important concept

xiaoxiao2021-03-06  55

First, the C language pointer 1. Pointer Description Pointer is an address variable containing another variable. (1) INT * P P is a pointer and points to a shaping number. (2) INT * p () P is a function that returns a pointer to integer. (3) INT (* p) () P is a pointer, which points to a function, which returns an integer. (4) INT * P [] P is an array that each element of the array is a pointer to an integer. (5) INT (* P) ​​[] P is a pointer, the pointer points to an array, and each element of this array is an integer. (6) INT * (* P) ​​() P is a pointer, which points to a function, which returns a pointer to integer.

2. Initialization (assignment address) (1) of the pointer (1) assigning the address of the symbol & quasi-variable (including the structural variable, the first element of the array); (2) assigns the array name to the pointer; (3) put the function name Assign a pointer to the pointing function; (4) Dynamic allocation memory: struct c {double r, i;}; struct c * p; p = (struct c *) malloc (Struct C)); 3. Pointer Array, function relationship (1) For one-dimensional array INT A [I] or pointer INT * A i pointing to A [I] (2) For string char s [i] or pointer char * s s i pointing I-th character S [I] (3) For two-dimensional array INT A [i] [j] * a j point to A [0] [j] * (A i) pointing a [i] [0] * (A I) J point to a [i] [j] example: for a [2] [3] = {1, 2, 3, 4, 5, 6,}; * (* (* (A 1) 1) = 5; (4) For strings array char p [i] [j] or character type pointer array char * p [i] * p j points to the first character of the 0th string * (P i) Points to the first character * (P I) J to the i-th string to point to the JO word of the i-th string: for * p [] = {"ABC", "DEF"} ; * (* (P 1) 1) = 'e'; example: for char p [] [3] = {"abc", "def"}; * (* (p 1) 1 ) = 'E'; (5) For pointer array int * a [i] A [i] pointing to variable I 即 * a [i] = variable I or A [i] = & variable I (6) for structural Struct XY {INT X; INT * Y} * p; p is a pointer (* p) .x or p-> x represents X or P-> Y is a pointer Y. Value (address) * (* p) .y or * p-> y is the content & (* p) .x or & p-> x referred to Y is the address (7) pointing to the function to the Void FUNC (CHAR * STR) {...}; // Defines a function void (* p) (char *); // Defines a function pointer P = func; // Let the pointer point to function (* P) ​​(" ... "); // Use the pointer P to call the function FUNC (8) points to multiple different The array of functions For Void function_1 () {...}; ... void function_4 () {...}; // defines four functions typef void (* menu_fcn) (); // Defines the pointer to the function Menu_fcn Command [4 ]; // Define the argument number command [0] = function_1; ... command [3] = function_4; // Let the pointer array points to four functions, Command [0] (); // Call one of the pointer arrays A function 4. Classification of pointers (1) Near pointers: Near pointers are 16-bit pointers, which only contains the offset portion of the address. Near pointers are used for a single data segment or code segment that does not exceed 64k bytes.

The data pointer generated in micro, small and medium compilation mode is a near pointer (default); the code pointer (pointer to the function) generated in micro, small, and in compilation mode is a near pointer (default). (2) The far pointer (FAR) far pointer is 32-bit pointers, the segment address and offset of the pointer are in the pointer. Can be used for any compilation mode. Reissue segment registers each time you use a far pointer. The target of the far pointer is not more than 64K, because the segment address does not participate in the calculation when the long finger is increased or decreased. Compiled, large and massive data pointers are far pools (default). (3) The giant pointer (huge) giant pointer is 32-bit pointers, the segment address and offset of the pointer are in the pointer. Can be used for any compilation mode. The goal of far pointer adding can exceed 64K. The giant pointer is a regular pointer. 5. Conversion of the pointer (1) Far pointer conversion into a giant pointer using the following function Void Normalize (Void Far * * P) {* p = ((long Far *) ((long) * p & 0xfff000f) ((long) * P & 0x0000FFF00 << 12));} 6. Use (1) Converting floating point numbers into binary number float ff = 16.5; unsigned char * cc; (float *) cc = & ff; // At this time, CC content is "" 00008441 "// ie CC first byte = 0; second byte = 0; third byte = 0x84; fourth byte = 0x41; (2) Convert binary number into floating point number Float FF Unsigned char * cc; cc = (unsigned char *) malloc (4); cc = (unsigned char *) & ff; * (cc 0) = 0; * (CC 1) = 0; * (CC 2 ) = 0x84; * (CC 3) = 0x41; // This time ff = 16.5 free (cc); two, C language function 1. User-defined function format type function name (form parameter table) parameter description {... ...} 2. Call mode (1) Passage value 1 Passing the called function is integer, long integer, floating point or double-precision variable. The called function is defined as a corresponding variable 2 Passing to the called function is a structural variable. The modified function depends on the structural variable as a structural parameter. 3 Passing a member of the called function is a member of the structural variable. The modified function is defined as the same variable as the member. (2) Equalization 1 pass to the called function is the address of the variable. The modified function is defined to define the pointer variable as a meticulous parameter. 2 Password is the address, ie array name, is called. Define an array or pointer variable as a ginseng. 3 Pass the address of the called function is the function name. The called function is determined to define the pointer variable of the function to the function. 4 The transmitted function is the address of the structure. The modified function is defined to define the structural pointer to the parameter. 3. Return (1) of the result of the function call (transmission method) Returns the value of the value required to be called is the same as the variable type of the received return value. (2) Returns It is the function that the pointer to be called is the pointer function, which is the same as the type of pointer points to the received pointer variable. (3) Do not return any value called the function is a VOID type.

Third, the information compression method of C language 1. Use the bit operator to compress the value of 5 data into one word (16 bits), assume that three of them (F1, F2, F3) are markers (true or false) Tropics; the fourth is called Type's integer, its value ranges from 1 to 12, need 4 bits of memory; the last integer called INDEX, the value range is from 0 to 500, requiring 9 digits . Define a integer variable for this: unsigned int packed_data can include this 5 values. The following figure is a bit domain allocation. TYPE INDEX F1F2F3 ┌ - ┐┌──────── ┐ ┌┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┐ └┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┘ Place the 4 low values ​​of N in the Type field of packed_data, with the following statement: packed_data = (Packed_Data & ~ (0xf << 9)) | (N & 0xF) << 9); Side or Symbol | Left It is set the TYPE domain 0, and the left is 9 bits to the TYPE domain after the low 4 digits of N is taken. Extract the value from the TYPE domain of Packed_Data and give it the statement it gives N is: n = (packed_data >> 9) & 0xf; 2. Define a structure called packed_struct using a bit domain structure (1), including 5 members Struct Packed_Struct {UNSIGNED INT F1: 1 Unsigned Int F3: 1; Unsigned Int Type: 4; UNSIGNED INDEX: 9;}; (Note: In the structure, it can also be placed in a normal data type, such as Char C; Wait) (2) Define a variable struct packed_struct packed_data; (3) Place the PACKED_DATA's TYPE field in the low position of N; (4) extract the Type field from Packed_Data (move it as required To the low), give it N, use statement n = packed_data.type; 3. Use a joint (1) an unsigned integer number with a structure (which contains many unsigned variables) share a storage area, when no symbol After the integer is assigned, you can obtain the value of the dominant variable. For example, the definition of a joint union {unsigned equi; struct {unsigned boot: 1; unsigned copr: 1; unsigned rsize: 2; unsigned vmode: 2; unsigned dnum: 2; unsigned: 1; unsigned cnum: 3; unsigned gnum: 1 Unsigned: 1; unsigned pnum: 2;} beq;} EQ; When the BIOS INT 11H is interrupted, the value of the AX is assured to EQ.Equi, and the PC can be obtained from eq.beq.boot. Information; information from EQ.BEQ.COPR gets the PC has floating point arithmetic components.

... (2) Two structures sharing the same storage area, for example: Union Regs struct WordRegs {UNSIGNED INT AX, BX, CX, DX, SI, DI, CFLAG, FLAGS}; Struct Byteregs {UNSIGNED CHAR AL, AH , BL, BH, CL, CH, DL, DH}; union regs {structure WordRegs x; struct byteregs h;} four, bit operation 1. Number of codes - complement (1). Positive completion and original code with. (2). The complement of the negative number is 1 first (symbol bit) is 1; 2 The remaining original code bits are inversely reflecting; 3 then add 1.2 for the entire number (1) Take some byte examples A & 0x00FF in a number to get the low byte of A, and the A & 0xFF00 gets a high byte of A. ┌ - ┬ - ┬──────────────────────────────── ─te │ ├ │ 补 码 │ ├─┼───────────────────────────────── ─ ─ ┤ │ │ │0x2cac │0010110010101100│ │ │ │ 00FF │00000000001111111│ ├─ ┴──────────────────────────────────────────────────────────tes iX00AC │0000000010101100│S │ Computing Results │ │ │ └─────────────────────────────) 1 Certain A | 0x0F Make a low 4 digits of A Change to 1. ┌ - ┬ - ┬ - ─ ┬───── ─ │ │ │ 十 进 │ 补 补 │ ├─┼──────────────── ─ ─ ┤ │ │ │0x0030 │0000000000110000 │ │ │ 000f │0000000000001111│ ├─ ┴───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── ─ ─ ─ ─ ─ ─ ─ ─ Result │ │ │ └ - ─ ┴ - ─ ─ ───── ─ ┘ (3). Turn a specific location to the plot A ^ 0x000F Make a low 4 bits of A (0 change 1 1 change 0).

┌ - ┬ - ┬ - ─ ┬───── ─ │ │ │ 十 进 │ 补 补 │ ├─┼──────────────── ─ ─ ┤ │ │ │ 0x007A │00000000000000001111│ │ │ │ 0x000F │0000000000001111│ ├─ ┴───────────────────────────────────────────────────────── Computing Results │ │ │ └ - ─ ─ ─────────── ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ = a ^ 0x02; // (0x02 = 00000010), the difference is "the same value of 0" (5). Example of the value of the two (integer) value A = a ^ b; b = B ^ a; A = a ^ b; // Three steps make the value of A, B to change 3. Special purpose (1) of the shift operation (1). A number of except 2 (right shift 1) case A >> 2 Make a 4 amplifiers for Signed A = -8, A >> 2 a = -8 ┌─┬─┬─┬─┬─┬─┬─┬─┐ │1 │1 │1 │1 │1 │ 0 │0 │0 │ └ - ┴─┴─┴─┴─┴─┴─┴─┘ ├─┬─┐─> └───── ┌─┬─┬─┬─┬─┬─┬─ ┬ - ┐│1 │1 │1 │1 │1 │1 │1 │0 │ └─┴─┴─┴─┴─┴─┴─┴─┘ A = -2 2 For unsigned a = 248, a> > 2 a = 248 ┌─┬─┬─┬─┬─┬─┬─┬─┐ │1 │1 │1 │1 │1 │0 │0 │0 │ └─┴─┴─┴─┴─┴ ─ ┴─┴─┘ └──── ┐─> └─── ┐ ┌─┬─┬─┬─┬─┬─┬─┬─┐ │0 │0 │1 │1 │1 │1 │1 │0 │ └ - ┴─┴─┴─┴─┴─┴─┴─┘ ┘ 0─ ┴─┘ A = 62 (2). Multiply by 2 (left to 1 bit) The Signed is the same as the UNSIGNED variable, it must be added to 0. (3) Position X & = ~ (1 "N) from the right of X (1" n); if x is long, X & = ~ ((long) 1 "n); (4) will Right to the n (n> = 0) position 1 x | = 1 "n; if X is a long prototy number, X | = (long) 1" N;

5. Method for accessing the CPU register 1. Use the joint regs, and the function int86 () / int86X () / INTR () REGS is used to transfer data to each register when performing a DOS soft interrupt call or returns from each register. value. Union Regs Screening Struct Struct WordRegs Byteregs ┌ ┌ - ────────────────────────────── │ │ 1 Byte │ │ │ AX ──────────── ─ ┴─ 2 Bytes │ │ │ h │ │ │ ├─────────────────────────────────────────────────────────────────────── │ │ │ │ ├─────────── │ │ │ │ │ │ │ │ ─ ─ ┤ │ │ │ CH │ │ ─────── ─ ┼──── ┤ │ │ │ │ DL │ │ │ DX ───── ┤ │ │ │ DH │ Union Regs - ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── │ │ │ │ │ │ │ ├──────── │ │ │ │ │ │ │ │ │ │ │ │ │ ├─────────────── ─ ─ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ├────── │ │ │ │ │ │ │ │ │ │ │ │ └ └─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── ┘ │ X Two Structure Variables H │ └─────────────────────────────Y2. Using Dummy Volume and Function GenInterrupt () Turbo C Allow Use the fake variable to access the corresponding 8086 register. There are two types of pseudo variables. 1 unsigned int: _AX, _BX, _CX, _DX, _CS, _DS, _SS, _S, _SP, _BP, _DI, _SI 2 unsigned char: _AL, _AH, _BL, _BH, _CL, _CH, _DL, _DH

Six, C language uses memory and registers 1. Segment and segment register CS used to store segment address of the code segment; DS is used to store the segment address of the global variable and static variable (data segment); SS is used to store local The parameter address of the parameter (stack) is located. In addition, there is a stack, which is a dynamically allocated memory. 2. The usage of micromold compilation period has only one segment, from the bottom to the bottom of this loading code, static variables, and global variables, heap. From low-loading stack. 3. The usage data, stack, and nests of small mode compilation period share a segment, and the code is used in a segment, and there is a distortion (access to FAR pointer). 4. The mode has multiple code segments in the usage of the mode compilation period, and the rest is the same as the small mode. Function pointer with a FAR pointer. 5. Compact mode compilation period usage code, static data, stack, stack (only far from a distortion) each has its own paragraph. The total amount of static data must not exceed 64K. 6. The use of the large model compilation period static data, heap, stack allocation is the same as the compact mode; the assignment of the code segment is the same. The data pointers and function pointers are far pools. The total amount of static data must not exceed 64K. 7. The usage of the mass compilation period is placed in different segments. The static data from different source files is also placed in different segments, and only the stack is together. 8. Running library function allocated memory: General memory area (outside data segment) is assigned with _fmalloc () to get 32-bit pointers ├─────────── ─ ─ ┤64│ (unused memory │ Assign by malloc () to get 16-bit displacement address KB ├───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── ─ ─ ─ Static variable │ ├──────────── ┤ ┤, use C language to write interrupt service procedures (if the interrupt service program does not involve the interrupt chain and its own re-entry problem.) --- Turbo C 1 The function type is the interrupt service program for Interrupt definition: #include void interrupt function name (BP, DI, Si, DS, ES, DX, CX, BX, AX, IP, CS, FLAGS); Unsigned Int BP, DI, Si, DS, ES, DX, CX, BX, AX, IP, CS, FLAGS; 2. You must first keep the original interrupt function address void interrupt (* reserved function name) (); reserved function name = getVect (0x interrupt number) 3. Replace the original program setVect (0x interrupt number, function name) with a custom interrupt service program in the main function; 4. Activate custom interrupt service programs in the main function (1) first set it. The value of the register (with a pseudo variable), (2) GenInterrupt (0x interrupt number); if the timing interrupt program is replaced, 18.2 interrupts are generated per second due to the timer in the PC, and 18.2 new Interrupt procedures. 5. Afterwards, the original interrupt function address will be replaced in the vector table in the meter, and the settlement (0x interrupt number, reserved function name);

转载请注明原文地址:https://www.9cbs.com/read-87841.html

New Post(0)