From

xiaoxiao2021-03-06  114

Word Source VC World - C language classroom

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

Function pointer variable

In the C language, a function always occupies a continuous memory area, and the function name is the first address of the memory area occupying the function. 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. 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);

Pointer type function

As we introduced earlier, the so-called function type refers to the type of function returns. The return value of a function is allowed in the C language is a pointer (ie address), which is called a pointer type function. The general form of defining the needle function is: Type Design * Function Name (Distake) {... / * Function * /}

Such as: int * ap (int x, int y) {... / * function body * /}

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 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 .......

The pointer array can also be used as a function parameter. In this main function, a pointer array name is defined, and the NAME is initialized to assign. Each element points to a string. Then use Name as the real parameter modified pointer type function DAY NAME, and the array name NAME is given to the mediate amount NAME, and the input integer I is assigned to the form of the parameter n. Two pointer variables PP1 and PP2, PP1 are assigned to Name [0], and PP2 is assigned to Name [n], ie, * (Name N). Decide of the conditional expression to return to the PP1 or PP2 pointer to the pointer variable PS in the main function. Finally outputs the value of I and PS.

Pointer array as a pointer function parameter main () {static char * name [] = {"Illegal day", "Monday", "Tuesday", "Wednesday", "Thursday", "Saturday", "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);} Enter 5 national names and output it in alphabetical order. #include "string.h" main () {void sort (char * name [], int N); void print (char * name [], int N); static char * name [] = {"China", " American, "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 0) k = j; if (k! = i) {Pt = Name [i]; Name [i] = Name [k]; Name [k] = pt;}} void Print (char * name [], int N) {INT i; for (i = 0; i

The main function introduced earlier is without parameters. Therefore, the brackets after Main are empty. In fact, the main function can take parameters, this parameter can be considered as the formal parameter of the main function. The C language specifies that the parameters of the main function can only have two, and these two parameters are written as Argc and Argv. Therefore, the function of the main function can be written as: Main (Argc, Argv) C language also specifies that argc (first ginseng) must be an integer variable, and Argv (second ginseng) must be a pointer to string. Array. After adding the design, the function of the main function should be written as: main (argc, argv) int Argv; char * argv []; or is written: main (int Argc, char * argv []) Because the main function cannot be Other functions are called, so it is impossible to obtain actual values ​​inside the program. So, where do the real gate value give the main parameter? In fact, the parameter value of the main function is obtained from the operating system command line. When we want to run an executable file, type the file name at the DOS prompt, and enter the actual parameters to send these real parameters to the metall paramesis.

The general form of the command line of the DOS prompt is: c: /> executable file name parameter parameter parameters ...; but should pay special attention to the parameters in the two ginseng parameters in the command line, the parameters in the position are not one or one of. Because, the metabology of Main is only two, and the number of parameters in the command line is not limited. The ARGC parameter represents the number of parameters in the command line (Note: The file name itself is also a parameter), and the value of the argc is automatically given by the system by the system by the system when entering the command line. For example, there is a command behavior: C: /> E6 24 Basic DBase Fortran Since the file name E6 24 itself is also a parameter, there are four parameters, so the value of the ARGC is 4. The argv parameter is the first address of the string pointer argument, and its element values ​​are the first address of each string in the command line (parameters in string processing). The length of the pointer array is the number of parameters. The initial value of array element is automatically given by the system. 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. Main () {static char * ps [] = {"Basic", "DBASE", "C", "fortran", "pascal"}; char ** PPS; INT i; for (i = 0; i <5 i ) {pps = ps i; printf ("% s / n", * pps);}} This program is programmed with pointer variables to pointers and outputs multiple strings.

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

New Post(0)