13.1 pointer to pointer
example:
int i; int *pi; int **ppi; printf( "%d\n", ppi ); //If ppi is an automatic variable, it is not initialized and will allow a random value. If it is a static variable, print 0 //Before ppi is initialized, the contents of its storage unit are unknown printf( "%d\n", &ppi ); //Print out the address of the storage unit of ppi. This value is useless *ppi = 5; //Error, cannot perform an indirect access operation on an uninitialized pointer ppi = π //ppi is initialized to a variable that points to pi *ppi = &i; //Initialize pi as a pointer to variable i. pi is accessed indirectly through ppi. /* The following three statements have the same effect */ i = 'a'; *pi = 'a'; **ppi = 'a'; int ***pppi; //Access levels can also be increased. Only when it is really necessary, multi-layer access can be used, otherwise the sequence will become larger, slower and more difficult to maintain.
13.2 high level statement
Tip: every function in the program is located in a certain location in memory, so it is legal to have a pointer that only wants that location.
example:
int *f; //Declare the expression * f as an integer. Then it is inferred that f is a pointer to an integer. int *a, b; //Here, only variable a is declared as a pointer and b is an integer variable. int *k(); //The expression * K() executes the function call operator () first, so K is a function and its return value is a pointer to an integer. int (*kk)(); //The first bracket grouping is performed first, which is an indirect access before the function call. Make kk a function pointer. The function it points to returns an integer value int *(*kkk)(); //kkk is also a function pointer. The return value of the function it points to is a pointer to an integer. int *c[]; //First perform the subscript operation. c is an array whose element type is a pointer to the integer. int cc()[]; //Illegal, function can only return scalar value, not array int ccc[](); //illegal int (*x[])(); //First evaluate the expression in the grouped parentheses and then the function caller, so x is an array whose element is the function pointer. The return value of the function pointed to is an integer value. int *(*xx[])(); //xx is an array whose element is a function pointer, and the return value of the function it points to is a pointer to an integer.
13.3 function pointer
Here are two common uses of function pointers: jump table and passing them as parameters to another function.
Note 1: like other pointers, function pointers must be initialized to point to a function before indirect access.
Note 2: when a function name is used, it is always converted to a function pointer by the compiler. The pointer points to the location of the function in memory. Then the function call operator () calls the function and executes the code starting at this address
Function initialization instance:
int f( int ); int (*pf)( int ) = &f; //Declare and initialize function pointer pf //There is & no need here. When a function name is used, it is always converted into a function pointer by the compiler& Only the description conversion operation is displayed. int ans; /* The following three statements have the same effect */ ans = f ( 2 );//The function name f is first converted into a function pointer, pointing to the location of the function in memory. Then the function call operator calls the function and executes the code starting at this address ans = (*pf)( 2 ); ans = pf ( 2 );
13.3.1 callback function
Let's first look at a function to find a specific value in the previous one-way linked list:
node *search( node *pnode, int const value ) { while( pnode != NULL ){ if( pnode->value == value ) break; pnode = pnode->link; } return pnode; }
The above implementation method can only search the linked list of integers. Make the lookup function independent of the type of lookup, so that it can be used in the linked list of values of any type.
/* Find a specified value in a single linked list. One of its parameters is a pointer to the first node of the chat table. A pointer to the function to be searched and a function pointer to the function to be compared */ node *search( node *pnode, void const *value, int (*compare)( void const *, void const *) ) { while( pnode != NULL ){ if( compare( &pnode->value, value ) == 0 ) //Equal returns 0 for compatibility with comparison functions in the standard library. break; pnode = pnode->link; } return pnode; } int com_ints( void const *a, void const *b ) { if( *(int *)a == *(int *)b ) return 0; else return 1; } //Call like this target_node = search( root, &target_value, com_ints );
13.3.2 transfer form
example:
switch( oper ){ case add: result = add( op1, op2 ); break; case sub: result = sub( op1, op2 ); break; ...//Suppose there are many operations, which are omitted here //Note: it is a good code style to separate the specific operation from the function implementation, the specific operation and the selection operation. } /* In order to use the switch statement, the representation operator code oper must be an integer. If it is a continuous integer starting from zero, it can be realized by conversion table. The conversion table is an array of function pointers Creating a transformation table requires two steps. 1,Declaration initializes a function pointer array and ensures that the prototypes of these functions are declared before the function pointer array 2,use */ double add( double, double ); double sub( double, double ); double mul( double, double ); ... double (*oper_func[])( double, double ) = { add, sub, mul,...... }; result = oper_func[oper]( op1, op2 );
**Note: * * when using function pointer array, be sure to check whether the array is out of bounds.
13.4 command line parameters
Write parameters in the command line to start a program execution. The first parameter is the name of the program.
13.4.1 passing command line parameters
The main function of c program has two formal parameters. The first is argc, which indicates the number of command line parameters. The second is usually called argv. It points to a set of parameters, which is essentially an array. The elements in it are pointers to a parameter text.
If the program needs to access the command line parameters, the main function adds these parameters argc and argv when declaring.
One line command example: CC - C - O main c insert. c -o test argc = 7;
cc is the name of the program. When the program has several groups of different options to start, the program checks the first parameter, determines which name to start, and selects the start item.
/* A program that prints its command-line parameters. Distinguish the command-line programs here from those above */ #include <stdlib.h> #include <stdio.h> int main( int argc, char **argv ) { /* Print command line parameters, program name skipped */ while( *++argv != NULL ){ printf( "%s\n", *argv); } return EXIT_SUCCESS; }
13.5 string constants
A string constant is a pointer constant when it appears in an expression. The array name is also a pointer constant when used in an expression. We can make subscript reference to the array, indirect access, and pointer operation. These operations also apply to string constants.
example:
"xyz" + 1; //The result is y *"xyz" ; //The result is x "xyz"[2]; //The result is z