From

xiaoxiao2021-03-06  102

Word Source VC World - C language classroom

Self-archiving, also dedicated to the same Delphi programmer as me

The pointer is a data type widely used in the C language. It is possible to process memory addresses like assembly languages, thereby compiling and efficient programs. The required memory unit can be found according to the number or address of the memory cell, so this address is usually referred to as a pointer. The content of the memory cell and the content of the memory cell is two different concepts. You can use a popular example to explain the relationship between them. For a memory unit, the address of the unit is a pointer, where the data stored is the content of the unit. Strictly speaking, a pointer is an address and is a constant. And a pointer variable can be given a different pointer value, which is a variable. However, the pointer variable is often referred to as a pointer. In order to avoid confusion, we agree that: "pointer" means the address, the constant, "pointer variable" means a variable of the value of the address. The purpose of defining a pointer is to access memory cells through a pointer. Because array or functions are continuously stored. The array or function is found by accessing the argument variables, and the array or function is found. As long as the pointer variable is given the first address of the array or function.

Type of pointer variable

Types of pointer variables include three contents: (1) pointer type description, that is, the defined variable is a pointer variable; (2) pointer variable name; (3) data type of variable pointed to by the variable value (pointer). Its general form is: Type Design * Variable Name; where * indicates that this is a pointer variable, the variable name is the pointer variable name defined, the type specifier indicates the data type of the variable pointed to this pointer variable. For example: int * p1; indicating that P1 is a pointer variable, its value is the address of a integer variable. Or say P1 points to a integer variable. As for the P1, which integer variable is pointed to, it should be determined by the address given to P1. Another example is: StAIC INT * P2; / * P2 is a pointer variable to static integer variable * / float * p3; / * p3 is a pointer variable to floating point variable * / char * p4; / * P4 is a character variable The pointer variable * / It should be noted that a pointer variable can only point to the same type of variable, such as P3 can only point to floating point variables, and point to a floating point variable, sometimes points to a character variable.

Assignment of pointer variables

The pointer variable must give a specific value. Unfameted pointer variables cannot be used. The assignment of the pointer variable can only give an address and will never give any other data. The address operator & to represent the address of the variable in the C language. Its general form is: & variable name; if & a address of the variable A, & b represents the address of the variable B. The variable itself must be described in advance. There is a pointer variable P in which the integer variable is provided. To assign P, the address of the integer variable A can be given below: (1) Method for initialization of pointer variable INT A; int * p = & a; (2) assignment Method of statement int a; int * p; p = & a; not allowing a number to impart pointer variables, so the following assignment is wrong: int * p; p = 1000; Cannot be added before being assigned pointer variables "The specifier, such as writing * p = & A is also wrong

Calculation of pointer variables

The pointer variables can be made, but the types of operations are limited. It can only perform assignment operations and partial arithmetic operations and relationships. Pointer operator

(1) Take address operators & Take Address Operators & Yamett Operators, which combined with right-to-left, and its function is the address of the variable. In the Scanf function and the previous introduction of the pointer variable assignment, we have known & operators.

(2) Take the content operator * Take the content operator * is an unicamed operator, its combination is from right to left, used to represent variables referred to by pointer variables. The variables followed by the * operator must be a pointer variable. It should be noted that the pointer operator * and pointer variable descriptions are not one thing. In the pointer variable description, "*" is a type specifier, indicating that the variables thereafter are the pointer type. The "*" appearing in the expression is an operator to indicate the variable referred to in the pointer variable. Main () {Int A = 5, * P = & a; Printf ("% d", * p);} ... indicates the address of the integer variable A. This statement represents the value of the output variable A. 2. Computing of pointer variables

(1) Assignment operation

The assignment calculation of pointer variables has several forms: 1 pointer variable initialization assignment, the previous introduction.

2 Give the address of a variable to the pointer variable to the same data type. For example: INT A, * Pa; PA = & a; / * Give an address of the integer variable A to the integer pointer variable PA * /

3 Assign a value of a pointer variable to another pointer variable to the same type variable. Such as: int A, * PA = & A, * PB; PB = Pa; / * Give a pointer variable PB * / due to PA, PB is a pointer variable to integer variables, thus mutual assignment.

4 Give the first address of the array to the pointer variables of the array. For example: INT A [5], * Pa; PA = a; (Number name name represents the first address of the array, so that the pointer variable PA to the array) can also be written as: PA = & a [0]; / * array The address of an element is also the first address of the entire array, and it is also possible to give PA * / of course the method of initial assignment: INT A [5], * PA = A;

5 Give the first address of the string to point to the pointer variable of the character type. For example: char * pc; pc = "c language"; or by initial assignment method is written as: char * pc = "c language"; herein should be noted to load the entire string into pointer variables, but The first address of the character array stored in the string is loaded into the pointer variable. Also described later.

6 Gring the entry address of the function to the pointer variable that points the function. For example: int (* pf) (); pf = f; / * f is a function name * /

(2) Adding and subtracting the arithmetic operation

For pointer variables pointing to array, you can add or subtract an integer n. Setting PA is a pointer variable pointing to array A, then PA N, PA-N, PA , Pa, PA -, - PA operations are legal. The meaning of the pointer variable or minus an integer N is to move the current position (point to a array element) pointing forward or backward. It should be noted that the array pointer variable moves forward or rearward and adds 1 or minus 1 in concept. Because arrays can have different types, the length of the byte lengths of various types of array elements are different. If the pointer variable plus 1, the backward moves 1 position indicates the first address of the pointer variable points to the next data element. Instead of adding 1 on the basis of the original address. For example: INT A [5], * Pa; PA = a; / * Pa pointing to array a, also pointing to A [0] * / PA = PA 2; / * PA pointing A [2], that is, the value of Pa & pa [2] * / The addition and decrease in the pointer variable can only be made to the array pointer variable, and the pointer variable to other types of variables is not meaningful. (3) The operation between the two pointer variables can only be operated between the two pointer variables to the same array, otherwise the operation is meaningless.

1 The difference between the two pointer variables and subtraction of the two pointer variables is the number of elements that differ between the two pointers index group elements. In fact, the difference between the two pointer values ​​(addresses) is then divided by the length of the array element (byte). For example, PF1 and PF2 are two pointer variables to the same floating point array. The value of PF1 is 2010 h, and the value of PF2 is 2000 h, and the floating point array accounts for 4 bytes, so the result of PF1-PF2 is ( 2000H-2010H) / 4 = 4, indicating a difference between PF1 and PF2. Two pointer variables cannot be added. For example, what does PF1 PF2 mean? There is no practical meaning. 2 Two pointer variables are performed to refer to the relationship between the two pointer variables of the same array, which can represent the relationship between their index group elements. For example: PF1 == PF2 indicates that PF1 and PF2 points to the same array element PF1> PF2 indicate that PF1 is in high address position PF1

The pointer variable can also be compared with 0. Set P as a pointer variable, p == 0 indicates that the P is an empty pointer, which does not point to any variable; p! = 0 means P is not empty pointer. The empty pointer is obtained by assigning 0 values ​​to the pointer variable. For example: #define null 0 int * p = null; 0 values ​​for pointer variables and non-assignment are different. When the pointer variable is not assigned, it can be any value, it is not possible. Otherwise it will cause an unexpected error. After the pointer variable is 0, it can be used, but it does not point to the specific variable. Main () {Int A, B, C, * PMAX, * Pmin; Printf ("Input Three Numbers: / N"); Scanf ("% D% D% D", & A, & B, & C); if (A > b) {pMax = & a; pmin = & b;} else {pmax = & b; pmin = & a;} if (c> * pmax) PMAX = & C; IF (c <* pmin) Pmin = & C; printf ("max =% d / nmin =% d / n ", * pmax, * pmin);

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

New Post(0)