Chapter 6: Pointer
Introduction to the pointer is a data type widely used in the C language. Using the pointer programming is one of the most important style of C language. The use of pointer variables can represent various data structures; it is convenient to use array and string; and can process memory addresses like assembly languages to compile the internal and efficient programs. The pointer greatly enriches the function of the C language. The learning pointer is the most important ring in the C language, and whether it can correctly understand and use the pointer to whether we have a sign of the C language. At the same time, the pointer is also the most difficult part in the C language. In addition to understanding the basic concepts in the study, it must be multi-programmed and debugged. As long as you do this, the pointer is not difficult to master. The basic concept of the pointer is in the computer, and all the data is stored in the memory. Generally, one byte in the memory is called a memory cell. The number of memory cells occupied by different data types is not equal. If the integer amount is 2 units, the character quantity accounts for 1 unit, etc., in the second chapter Detailed introduction. In order to properly access these memory cells, the number must be made for each memory unit. The memory unit can be accurately found according to the number of a memory cell. The number of the memory cell is also called the address. Since the required memory unit can be found according to the number or address of the memory cell, 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. When we go to the bank to deposit, the bank staff will find our deposit slip according to our account. After finding it, it is found to write deposits on the deposit order, withdraw the amount. Here, the account is the pointer of the memory, and the number of deposits is the content of the memory. For a memory unit, the address of the unit is a pointer, where the data stored is the content of the unit. In the C language, a variable is allowed to store the pointer, which is called a pointer variable. Therefore, the value of a pointer variable is the address of a memory cell or a pointer called a memory unit. In the figure, character variable C is provided, its content is "K" (ASCII code is a decimal number 75), and C occupies the 011A unit (address hexadecimal representation). It is provided with a pointer variable P, which is 011A. This condition we call P pointing to variable C, or P is pointed to the variable C. Strictly speaking, a pointer is an address and is a constant. And a pointer variable can be given different pointer values, which is changed. 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. Since the value of the pointer variable is an address, then this address can be the address of the variable, but also the address of other data structures. What is the meaning of the first address stored in a pointer variable or a function? 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. In this way, any array, where the function can be expressed in a pointer variable, as long as the pointer variable gives an array or the first address of the function. In doing so, the concept of the program will be very clear, the program itself is also refined, efficient. In C language, a data type or data structure often occupies a set of continuous memory cells. With the concept of "address", it does not describe a data type or data structure, and "pointer" is actually an address, but it is the first address of a data structure, it is a "pointing" a data structure. And thus concept is more clear, indicating more clear. This is an important reason for introducing the concept of "pointer".
Type of pointer variable Description Type Description of the pointer variable includes three contents: (1) pointer type description, that is, the defined variable is a pointer variable; (2) pointer variable name; (3) variable pointed to by the variable value (pointer) Data type. 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. The assignment pointer variable of the pointer variable is the same as normal variables, but also not only to define instructions before use, but must give a specific value. Unfailed pointer variables cannot be used, otherwise the system will cause system confusion, or even crash. The assignment of the pointer variable can only give an address, and it will never give any other data, otherwise an error will cause an error. In the C language, the address of the variable is assigned by the compilation system, which is completely transparent to the user, and the user does not know the specific address of the variable. 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 the error pointer variable of the wrong pointer variable, but the species of its operations are limited. It can only perform assignment operations and partial arithmetic operations and relationships. 1. Pointer operators (1) Take address operators & Take address operators & are single-purpose operators, their combinations are from right to left, and their functions are 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. The calculation of the pointer variable (1) Assignment operation of the assignment unit has the following form: 1 The pointer variable initializes assignment, which has been described earlier. 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 to 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, or it can also give PA * / of course method for initial assignment: INT A [5], * PA = a; 5 gives the first address of the string to point to character types Pointer variable. 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 subtraction arithmetic operation to point or minus 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 assignment pointer variable assignment judges and assigns the value and assigns the output result ... The description of the array pointer variable and the pointer variable that point to the array is called the array pointer variable. Before discussing the description and use of array pointers, we must clear a few relationships. An array is composed of a continuous memory unit. The array name is the first address of this continuous memory unit. An array is also composed of each array element (subscript variable). Each array element has several consecutive memory cells in its type. The first address of an array element refers to the first address of several memory cells it occupies. A pointer variable can be directed to an array or pointing to an array element that imparts the address of the array name or the first element. To make the pointer variable point to the first address of the I element to give it or give it an array name. There is a real number A, pointing to a pointer variable from A. From Figure 6.3 we can see that the following relationships: PA, A, & A [0] points to the same unit, which is the first address of array A, is also 0 The first address of the element a [0]. PA 1, A 1, & A [1] points to 1 element a [1]. Category A I, A I, & A [i] points to the I n [I]. It should be noted that PA is variable, and a, & a [i] is constant. Pay attention to the programming. Main () {Int A [5], I; for (i = 0; i <5; i ) {a [i] = i; printf ("a [% d] =% D / N", I, A [i]);} Printf ("/ n");} The main function defines a integer array and a integer variable cycle statement to the array assignment to print the value of each array ... Output wrap .... .. The general form of the array pointer variable description is: Type Design * Pointer Variable Name where the type indicator represents the type of the index group. From a general form, it can be seen that the pointer variables of the array and the description of the pointer variables to ordinary variables are the same. After introducing the pointer variable, you can use two ways to access the array element. The first method is the subscript method, that is, the array element is accessed in the form of a [i]. This method is used when the array occurs in Chapter 4. The second method is a pointer method, i.e., in the form of * (PA I), using indirect access to the array element. Main () {Int A [5], I, * Pa; PA = a; for (i = 0; I <5; I ) {* PA = I; PA ;} PA = a; for (i = 0; I <5; i ) {Printf ("a [% D] =% D / N", I, * Pa); PA ;}} The main function defines the integer array and pointer to point the pointer PA to the array A cycle will change the variable I The value is assigned to the array unit of the a [] pointed to the pointer PA to point the pointer PA to the next unit of A [] ... The first address cycle of the array A is output in the array mode of the array. All elements will point pointer PA points to the next unit of A [] .......... Next, another example, the example is the same as the above, but the implementation is different. Main () {Int a [5], I, * PA = a; for (i = 0; I <5;) {* PA = I; Printf ("a [% d] =% d / n", i , * PA );}} The main function defines an integer array and a pointer, and causes the pointer to an array A cycle to assign the value of the variable I to the array unit pointed to the array unit of the array unit in the array unit in the array A unit. At the same time, the pointer PA points to the next unit of a [] ............ The number of group names and array pointer variables for function parameters have introduced the parameters and meticulum The problem. This problem is easier to understand after learning the pointer variable. The number of group names is the first address of the array. The actual parameter gates are actually the address of the transfer array, and the shape is also directed to the same array. This is like the same items, which are both different from each other.
Similarly, the value of the pointer variable is also the address, the value of the array pointer variable is the first address of the array, and of course, as a function of the function. FLOAT AVER (Float * Pa); main () {float sco [5], av, * sp; int 1; sp = sco; printf ("/ ninput 5 score: / n"); for (i = 0; i <5; i ) scanf ("% f", & sco [i]); av = AVER (SP); Printf ("Average Score IS% 5.2F", AV);} Float Aver (FLOAT * PA) {INT i Float av, s = 0; for (i = 0; i <5; i ) s = S * PA ; AV = S / 5; Return AV;} Pointer variables pointing to the multi-dimensional array of pointer variables Take two-dimensional array as an example This section describes the pointer variables of the multi-dimensional array. First, the representation of the multi-dimensional array address is equipped with a total two-dimensional array A [3] [4] as follows: 0 1 2 34 5 6 78 9 10 11 Setting the first address of the array A is 1000, the first address of each subscript variable And its value is shown in the figure. In Chapter 4, the C language allows a two-dimensional array to decompose into multiple one-dimensional arrays. Therefore, array A can be broken down into three one-dimensional arrays, namely A [0], A [1], A [2]. Each one-dimensional array has four elements. For example, a [0] array contains A [0] [0], A [0] [1], A [0] [2], A [0] [3] four elements. The address of the array and array elements is shown below: A is a two-dimensional number of group names and the first address of the 2D array 0 row, equal to 1000. A [0] is the number of group names and first addresses of the first one-dimensional array, so it is also 1000. * (A 0) or * a is equivalent to A [0], which represents the first-dimensional array a [0] No. 0 element. Also 1000. & a [0] [0] is the 0 row of 0 rows of two-dimensional array A, which is also 1000. Therefore, A, A [0], * (A 0), * a, & a [0] [0] are equal. Similarly, A 1 is the first address of the two-dimensional array 1 line, equal to 1008. A [1] is the number of group names and first addresses of the second one-dimensional array, so it is 1008. & a [1] [0] is a 1 line 0 column element address of the two-dimensional array A, is also 1008. Therefore, A 1, A [1], * (A 1), & a [1] [0] is equivalent. This can be obtained: A I, A [I], * (A I), & a [i] [0] is equivalent. In addition, & a [i] and a [i] are also equivalent. Because the & a [i] cannot be understood as the address of Element A [I] in the two-dimensional array, there is no element a [i]. C language regulations, it is an address calculation method that represents an array A X-line address. As a result, we conclude: a [i], & a [i], * (a i) and a i are also equivalent. In addition, A [0] can also be regarded as a first address of the No. 0 element of one-dimensional array a [0], and a [0] 1 is an element of A [0] The first address, thereby obtaining a [i] j is a one-dimensional number of group J, the first address, which is equal to & a [i] [j]. A [i] = * (A i) is a [i] j = * (A I) J, due to * (A I) J is the i line J column elements of the two-dimensional array A First address.
The value of this element is equal to * (* (A I) J). [EXPLAIN] #define pf "% D,% D,% D,% D,% D, / N" main () {static int a [3] [4] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}; Printf (PF, A, * A, A [0], & A [0], & A [0] [0]); Printf (PF, A 1 , * (a 1), a [1], & a [1], & a [1] [0]); PrintF (PF, A 2, * (A 2), A [2], & a [2 ], & a [2] [0]); PrintF ("% D,% D / N", A [1] 1, * (A 1) 1); Printf ("% D,% D / N ", * (A [1] 1), * (* (a 1) 1));} II, the pointer variable of multi-dimensional array puts the two-dimensional array A into one-dimensional array a [0], A [ 1], after A [2] sets P to point to the pointer variable to the two-dimensional array. Can be defined as: int (* p) [4] It indicates that P is a pointer variable, which points to the two-dimensional array a or pointing to the first one-dimensional array a [0], its value is equal to A, A [0], or & a [0] [0], etc. And P I points to the one-dimensional array a [I]. From the previous analysis, * (p i) j is the address of the element of the two-dimensional array I line J column, and * (* (p i) j) is the value of the i line J column element. The general form of the two-dimensional array pointer variable description is: Type Design (* Pointer Variable Name) [Length] where "Type Descriptor" is the data type of the index group. "*" Indicates that the variables thereafter are pointer types. "Length" means that the two-dimensional array is decomposed into a plurality of one-dimensional array, the length of the one-dimensional array is the number of columns of the two-dimensional array. It should be noted that "(* pointer variable name)" is not less brackets on both sides. If the lack of parentheses, it is said that it is a pointer (introduced later in this chapter), and the meaning is completely different. [Explain] main () {static int A [3] [4] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}; int (* p) [4 ]; INT I, J; P = a; for (i = 0; i <3; i ) for (j = 0; j <4; j ) Printf ("% 2d", * (* (p i) J));} 'Description of the' Expain string pointer variable and definition of string pointer variable Description is the same as the pointer variable indicating the character variable. It can only be different from the assignment of the pointer variable. The pointer variable to the character variable should give the address of the character variable. Such as: CHAR C, * P = & C; indicating that P is a pointer variable to character variable C. And: char * s = "c language"; indicating that S is a pointer variable pointing to the string. Give the first address of the string to s. Please see an example below. Main () {char * ps; ps = "c language"; Printf ("% s", ps);} Run results are: C language, first define PS is a character pointer variable, then put strings The first address gives PS (which should write the entire string so that the compilation system sets the string into a continuous memory unit) and send the first address to the PS. Program: char * ps; ps = "c language"; equivalent to: char * ps = "c language"; all characters after the n characters in the string are output.
Main () {char * ps = "this is a book"; int N = 10; ps = ps n; Printf ("% s / n", ps);} Run results are: Book Input PS initialization That is, the character serial address is given to the PS. After the PS = PS 10, the PS points to the character "B", so the output is "BOOK". Main () {char ST [20], * ps; INT I; Printf ("INPUT A String: / N"); PS = St; Scanf ("% S", PS); for (i = 0; PS [ I]! = '/ 0'; i ) IF (PS [i] == 'k') {printf ("" there is a 'k' in the string / n "); Break;} IF (PS [i] == '/ 0') Printf ("TheRe IS NO 'K' IN THE STRING / N");} This example is to find a 'K' character in the input string. The following example is to point the pointer variable to a format string, in the Printf function, used to output the value of various address representation of the two-dimensional array. However, the format string is replaced with a pointer variable PF in the PrintF statement. This is also a method commonly used in the program. Main () {static int A [3] [4] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}; char * pf; pf = "% D, % D,% D,% D,% D / N "; Printf (PF, A, * A, A [0], & A [0], & a [0] [0]); Printf (PF, A 1 , * (a 1), a [1], & a [1], & a [1] [0]); PrintF (PF, A 2, * (A 2), A [2], & a [2 ], & a [2] [0]); PrintF ("% D,% D / N", A [1] 1, * (A 1) 1); Printf ("% D,% D / N ", * (a [1] 1), * (* (A 1) 1));} In the following example, the string pointer is used as the function parameters. Requires the contents of a string to another string and cannot use the strcpy function. The shape of the function CPRSTR is two character pointer variables. PSS points to the source string, PDS points to the target string. Expression: (* PDS = * PSS)! = `/ 0'cpystr (Char * PSS, Char * PDS) {while (* PDS = * PSS)! = '/ 0') {PDS ; PSS ;}} Main () {char * pa = "China", b [10], * Pb; Pb = B; CPYSTR (PA, PB); Printf ("String A =% S / NSTRING B =% S / N", PA In the above example, the program completed two work: First, copy the source characters pointed to by the PSS to the target character pointing to the PDS, and the second is to determine whether the replicated character is `/ 0 ', if Then indicate that the source string ends and no longer cycling. Otherwise, PDS and PSS are added to 1, pointing to the next character. In the main function, the CPRSTR function is called after the spectrum variable PA, PB is obtained, and the CPRSTR function is called after the determination value is obtained. These strings can be used in the main function and the CPRSTR function because the pointer variables PA and PSS, PB, and PDS are used to point to the same string.
You can also simplify the CPRSTR function to the following form: CPRSTR (Char * PSS, CHAR * PDS) {while (* PDS = * PSS )! = `/ 0 ');} That is, the movement and assignment of the pointer in one statement in. Further analysis can also be found that the US / 0 ''s ASCII code is 0. For the While statement, only the value of the expression is not 0, and 0 ends cycle, so it can also save "! =` / 0' " One judgment section, is written as the following form: CPRSTR (CHAR * PSS, CHAR * PDS) {while (* PDSS = * PSS );} The meaning of the expression can be interpreted as that the source character assigns the target character, the mobile pointer, if The value is not 0 cycle, otherwise the cycle is ended. This makes the program more concise. The simplified procedure is as follows. CPYSTR (CHAR * PSS, CHAR * PDS) {while (* PDS = * PSS );} main () {char * PA = "China", B [10], * Pb; PB = B; CPYSTR (Pa, PB ); Printf ("string a =% s / nstring b =% S / N", PA, PB);} Use string pointer variables to distinguish between character arrays and character pointer variables can be stored in strings And the operation. But both are different. Note the following questions when using: 1. The string pointer variable itself is a variable for the first address of the string. The string itself is stored in a continuous memory space headed by the first address and is ended as a string as a string. The character array is due to several array elements, which can be used to store the entire string. 2. Assignment assignment to the character number group, must be used in external types or static types, such as: static char st [] = {"c language"}; no such limit for string pointer variables, such as: char * ps = " C language "; 3. For string pointer method char * ps =" c language "; can be written as: char * ps; ps =" c language "; and alternatively: static char st [] = {" C LANGUAGE "}; Cannot be written as: char ST [20]; ST = {" c language "}; only assigns each element of the character array. From the above point, you can see the difference between the string pointer variable and the character array in use, and it can also be more convenient to use the pointer variable. As mentioned earlier, when a pointer variable is dangerous before the determination is not obtained, it is easy to cause errors. However, the direct assignment of the pointer variable is possible. Because the C system is assigned to the pointer variable to the determined address. Therefore, char * ps = "c langage"; or char * ps; ps = "c language"; all legal. The function pointer variable is specified in the C language, and a function always takes up a continuous memory area, and the function name is the first address of the memory area occupying the function. We can give this first address (or entrance address) to a pointer variable to point the pointer variable to the function. Then you can find and call this function through the pointer variable. We refer to the pointer variable of this pointing function as "function pointer variable". The general form of the function pointer variable definition is: Type spectriction (* Pointer variable name) (); "Type Descriptor" means the type of return value of the finger function. "(* Pointer Variable Name)" indicates that the variable behind "*" is the defined pointer variable. The last nicker indicates that the pointer variable is a function.
For example: int (* pf) (); indicating that PF is a pointer variable pointing to function portals, the return value of the function (function value) is integer. The method of implementing the function call in the form of a pointer is illustrated by an example. INT MAX (INT A, INT B) RETURN A; Else Return B;} main () {Int Max (int A, INT B); int x, y , Z; PMAX = max; Printf ("Input Two Numbers: / N"); Scanf ("% D% D", & X, & Y); z = (* pmax) (x, y); printf ("Maxmum = % D ", Z);} From the above program, the steps of the function pointer variable in the form of the function pointer are as follows: 1. First define the function pointer variable, as the 9th line of INT (* pmax) in the next program (* pmax); Define pmax as function pointer variables. 2. Give the entry address (function name) of the modulated function to the function pointer variable, such as the 11th line pMax = max; 3. Use the function pointer variable to call the function, such as the program 14 line z = (* pmax) (x, y); General Form of call functions is: (* Pointer Variable Name) (Real parameters) Use the function pointer variable should also be aware of the following two points: a. Function pointer variable cannot be arithmetic operation, this is the array pointer Variables are different. The array pointer variable adds an integer to move the pointer to the array elements behind or previous, and the movement of the function pointer is meaningless. b. The brackets of the two sides of "(* pointer variable name)" are indispensable, where * should not be understood as the evaluation operation, it is just a representation symbol. We introduced before the pointer function, the so-called function type refers to the type of function return value. The return value of a function is allowed in the C language is a pointer (ie address), which is called a pointer type function. Define the general form of the needle function to: Type Design * Function Name (Distread) {... / * Function * /} where the function name is added to the "*" indicated that this is a pointer function, that is, returned The value is a pointer. Type spectrines represent the data type pointed to the returned pointer value. Such as: int * ap (int x, int y) {... / * function body * /} indicates a pointer type function that returns a pointer value, which is returned to a integer variable. A pointer type function day_name is defined in the following example, and its return value points to a string. A static pointer array name is defined in this function. The Name array initialization assignment is eight strings, indicating each weeks and error tips, respectively. The meticulin N represents an integer corresponding to the week name. In the main function, the input integer i is used as the argument, and the DAY_NAME function is called in the printf statement and transmits the I value to the gum n. The return statement in the day_name function contains a conditional expression, and the n value is more than 7 or less than 1, returns the Name [0] pointer to the main function output error prompt string "Illegal Day". Otherwise returns the main function output corresponding week. The 7th line in the main function is a conditional statement, which is semantic, such as the input is a negative number (i <0), the abort process runs out. EXIT is a library function, exit (1) indicates that the program is exited after an error, and exit (0) means normal exit. Special attention should be paid to the difference between the function pointer variable and the pointer function. The difference in the writing and sense. If int * p () and int * p () are two completely different amounts. INT (* p) is a variable description, which means that P is a pointer variable to function portal. The return value of the function is an integer amount, and the brackets of both sides of (* p) cannot be less. INT * p () is not a variable description but a function description, indicating that P is a pointer function, its return value is a pointer pointing to an integer amount, * p on both sides do not parentheses.
As a function description, it is best to write the form parameters in parentheses, so that it is convenient to distinguish between variables. For the pointer type function definition, int * p () is only the function header portion, and should also have a function portion. Main () {Int i; char * day_name (int N); Printf ("Input Day no: / n"); scanf ("% D", & i); if (i <0) exit (1); Printf "DAY NO:% 2D ->% S / N", I, DAY_NAME (i));} char * day_name (int N) {static char * name [] = {"Illegal day", "Monday", " Tuesday, "Wednesday", "Thursday", "Saturday", "Sunday"}; Return ((N <1 || n> 7)? Name [0]: Name [n]);} The program is an integer between 1 to 7 by a pointer function, and outputs the corresponding week name. The description of the pointer array is a pointer array with an element value using an array. The array of pointers is a set of ordered pointers. All elements of the array array must be a pointer variable with the same storage type and point to the same data type. The general form of the pointer array description is: Type Design * An array name [array length] where the type specifier is the type of variable pointed to by the pointer value. For example: INT * PA [3] indicates that PA is a pointer array, which has three array elements, each element value, pointing to integer variables. You can usually be used to point to a two-dimensional array with a pointer array. Each element in the pointer array is given the first address of each line of the two-dimensional array, so it is also understood to point to a one-dimensional array. Figure 6-6 shows this relationship. INT A [3] [3] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; int * pa [3] = {a [0], A [1], A [2 ]}; int * p = a [0]; main () {INT i; for (i = 0; i <3; i ) Printf ("% D,% D,% D / N", A [i] [2-i], * a [i], * (* (A I) I)); for (i = 0; i <3; i ) printf ("% D,% D,% D / N ", * PA [i], p [i], * (p i));} In this case, PA is a pointer array, and the three elements point to each line of the two-dimensional array A. The specified array element is then output with a loop statement. Where * a [i] represents the I row 0 column element value; * (* (A I) i) represents the element value of the I row i column; * PA [i] represents the i row 0 column element value; due to P A [0] is the same, so P [i] represents the value of the 0 row I column; * (p i) represents the value of the 0 row I list. The reader can take a closer understanding of the various representations of the elements. It should be paid to the difference between the pointer array and the two-dimensional array pointer variable. Although both can be used to represent a two-dimensional array, its representation and significance are different. The two-dimensional array pointer variable is a single variable, and the parentheses in the general form "(* pointer variable name)" are indispensable. The pointer array type indicates a plurality of pointers (a set of ordered pointers) in the general form, "* pointed number group name" cannot have parentheses. For example: int (* p) [3]; represents a pointer variable to the two-dimensional array. The number of columns of the two-dimensional array is 3 or decomposed into a length of 3. INT * P [3] indicates that P is an array of pointers, and there are three subscript variables P [0], P [1], and P [2] are pointer variables. The pointer array is often used to represent a set of strings, and each element of the argument array is given the first address of a string.
The initialization of the pointer array points to the string is simpler. A set of strings are represented by, for example, in Example 6.20, a pointer array is used. Its initialization assignment is: char * name [] = {"Illagal Day", "Monday", "Tuesday", "Wednesday", "Thursday", "SURDAY", "SURDAY", "Sunday"}; complete this initialization assignment After that, Name [0] points to the string "Illegal Day", Name [1] refers to "quot; monday" ....... pointer array can also be used as a function parameter. In this main function, it is defined. A pointer array name, and the NAME is initialized. Each element points to a string. Then use Name as the real parameter function DAY NAME, and the array name Name is given to the argument Name, The integer i input is given to the parameter n. The two pointer variables PP1 and PP2 are defined in the DAY Name function, and the PP1 is given the value (ie * name) of Name [0], and the PP2 is given Name [ N] The value is * (Name N). Decide of the conditional expression to return PP1 or PP2 pointer to the pointer variable PS in the main function. The value of the I and PS is output. The pointer array is the parameter main () { Static char * name [] = {"Illegal day", "Monday", "Tuesday", "Wednesday", "Thursday", "SATURDAY", "Sunday"}; char * ps; int i; char * * Day name (char * name [], int N); Printf ("INPUT DAY NO: / N"); Scanf ("% D", & I); if (i <0) exit (1); ps = day Name (Name, I); Printf ("DAY NO: 2D ->% S / N", I, PS);} char * day name (char * name [], int N) {char * pp1, * PP2; PP1 = * Name; PP2 = * (Name N); return ((n <1 || n> 7)? PP1: PP2);} The following example requires input 5 national names and outputs after alphabetical order. In the previous example, an ordinary sorting method is used, and the position of the string is exchanged one by one. The physical location of the switching string is done by the string replication function. Repeated exchange will make the program execution speed slowly, The lengths of each string (national name) have increased the burden of storage management. These issues can be solved very well with the pointer number. All strings are placed in an array, and these The first address of the character array is placed in a pointer array. When two strings need to be exchanged, only the contents (addresses) of the corresponding two elements of the pointer must be exchanged, without exchanging the string itself. Two functions are defined in the program, one named Sort is sorted, and its shape is the pointer array name, which is a pointer to the array of each string to be sorted. The number of ginseng n is the number of strings. Another function name is Print, used to sort the output of the string, and its shape is involved in the formal parameter of the SORT. In the main function main, the pointer array Name is defined and the initialization assignment is defined. The Sort function and the print function are then called separately to complete the sorting and output. It is worth noting that in the SORT function, compared to two strings, the strCMP function is used, and the strCMP function allows the string of the comparison to appear in pointer.
Name [K] and Name [J] are each pointer, so it is legal. When the string is compared, only the value of the pointer array element is swapped without exchanging the specific string, which will greatly reduce the cost of time, and improve the operating efficiency. The programming is as follows: #include "string.h" main () {void sort (char * name [], int N); void print (char * name [], int N); static char * name [] = {" China, "America", "Australia", "France", "German"}; int N = 5; sort (name, n); print (name, n);} void sort (char * name [], int n) {char * pt; INT I, J, K; For (i = 0; i
It is shown in Figure 6.8: main (int Argc, char * argv) {while (argc -> 1) Printf ("% s / n", * argv);} This example is the input to the command line. Parameters If the executable file of the previous example is E24.exe, store it in the A drive. So the input command behavior: C: /> A: E24 Basic DBase Fortran runs the result to: BasicDBasefortRan This line has 4 parameters. When performing MAIN, the initial value of Argc is 4. The 4 elements of Argv are divided into four strings of the first address. Execute the While statement, each cycle ARGV value is reduced by 1, stop the loop when Argv is equal to 1, cyclic cycle, therefore, three parameters can be output. In the Printf function, since the print item * Argv is plus 1 printed, the first print is the string Basic referred to as Argv [1]. Second, the three cycles are printed separately two strings. The parameter E24 is the file name and does not have to be output. There are two parameters in the command line of the following example, and the second parameter 20 is the input N value. In the program * Argv value is a string "20", then use the function "ATOI" to switch it into a loop control variable in the While statement, and output 20 orientations. #include "stdlib.h" main (int Argc, char * argv []) {Int a = 0, n; n = atoi (* argv); while (n--) printf ("% d", A * 2);} This program is output from 0 to the output n number. Pointer to point to a pointer If a pointer variable is stored in another pointer variable, the pointer variable is called a pointer variable to the pointer. It has been introduced earlier, referred to as indirect access, referred to as interviews through pointer access variables. Since the pointer variable directly points to the variable, it is called a single stage interview. If the variable is accessed by pointing to the pointer variable to the pointer, secondary or multi-level interviews are constituted. In a C language program, the number of interviews is not clearly limited, but the interviewary level is too much to understand, and it is easy to make mistakes, so it is generally more than two interviews. Pointer pointer variable description of pointer to: Type Design ** Pointer variable name; for example: int ** pp; Indicates PP is a pointer variable, pointing to another pointer variable, and this pointer variable points to a integer amount . Here is an example to illustrate this relationship. Main () {int x, * p, ** pp; x = 10; p = & x; pp = & p; printf ("x =% d / n", ** pp);} P is one in the previous program The pointer variable points to the integer x; PP is also a pointer variable, which points to the pointer variable P. The writing of X is sent to the X of the PP variable is ** PP. The program last output X is 10. By the above example, the reader can learn the description and method of use of pointer variables to the pointer. The first definition of the pointer array PS is first defined in the following procedure. The PPS is a pointer variable that points to the pointer. In the 5 cycle, the PPS acquired PS [0], PS [1], PS [2], PS [3], and PS [4], as shown in Figure 6.10). This string can be found again through these addresses.