C and Pointers (9) Linked Lists and Pointers to Pointers

Linked list
1. A linked list is a collection of independent data structures that contain data. Each node in the linked list is connected together by a pointer. Usually, the nodes are dynamically allocated.
1) Each node in the singly linked list contains a pointer to the next node, and the pointer to the last node in the linked list is NULL.
2) The nodes in the linked list may be distributed in various places in the memory, and are not necessarily physically adjacent.
3) The singly linked list can traverse the linked list from the head node of the linked list to the tail node, but cannot traverse in the opposite direction.
4) The double-linked list contains a pointer to the previous node and a pointer to the next node, and the linked list can be traversed in both directions.
5) One of the two pointers of the head node of the double linked list points to the first node of the linked list, and the other points to the last node. If the linked list is empty, both pointers are NULL.

2. Singly linked list is a data structure that uses pointers to store values. Each node in the linked list contains a field for pointing to the next node in the linked list. In addition, there is an independent pointer pointing to the first node of the linked list. When the node is created With the method of dynamically allocating memory, the nodes are not necessarily physically adjacent, and the linked list is traversed according to the pointer, and the singly linked list can only be traversed in one direction.
3. The double linked list contains two link fields, one points to the next node of the linked list, and the other points to the previous node of the linked list, which can be traversed in both directions.

Function pointer
1. The length of the array must be specified when declaring the array. If the link attribute of the array is external or used as a parameter of a function, the length may not be specified when declaring.
2. The function pointer needs to be initialized before use. The & operator in the initialization expression can be omitted. The compiler implicitly converts the function name into a function pointer, and the & operator explicitly requires the compiler to perform type conversion.

3. Callback function, pass the function pointer as a parameter to other functions, and the latter calls back this function.
1) Many window systems use callback functions to connect multiple actions.
2) If the exact type of the callback function cannot be determined in the context, the parameter type can be declared as void*, and the parameter can be converted to the correct type in the callback function.

4. The transfer table is an array of function pointers. When declaring and initializing an array of function pointers, the order of the function names in the initialization list is the order of the array subscripts. Select the correct function pointer from the array, and the function call operator will execute the function body .

5. The subscript of the function pointer array is out of bounds, and the program may terminate in three places:
1) First, the memory with subscript out of bounds may be outside the memory allocated to the program. The operating system detects this error and terminates the program. If the program terminates, The error report is at the position where the subscript is out of bounds, but some operating systems will not terminate the program;
2) The program is not terminated, and the value of the illegally subscripted memory is extracted. The value may not be a valid address. At this time, the program terminates, and the address of the error report is random, the problem Debugging is more difficult;
3) If the value is a valid address in the program, the machine will execute the instruction of the virtual address obtained according to the illegal subscript. The operand address is terminated; 4) The value may also represent a valid instruction, and the program executes the instruction at the random address. If the random address happens to be inside a function, executing the function may modify some data, resulting in unknown behavior, and the return of the function The address is another random value, the computer will jump between each random address, the instruction destroys the call path, and the problem is more difficult to debug.
5) From the beginning, it should be ensured that the subscript of the transfer table is within the legal range.

Command line parameters
1, command line parameters are pointers to pointers Another useful place, in some systems, the user writes parameters in the command line to start the program, and the parameters are passed to the program.

2. The main function of C has two formal parameters, the first argc indicates the number of command line parameters, the second argv points to a set of parameter values, and each element points to a pointer to a parameter text.
1) If the program needs to access command line parameters, the main function needs to add these parameters when declaring.
2) The pointer array of argv, each element is a character pointer, the end of the array is a NULL pointer, and argv is declared as a pointer to a pointer to a character.
3) Some operating systems pass the third parameter to the main function, which is a pointer to the list of environment variables and values, refer to the operating system and compiler.

3. The first parameter of argv is usually the name of the program. The program obviously knows its own name, and this parameter is usually ignored.
1) However, if the program starts with several different options, this parameter cannot be ignored. For example, in a UNIX system, a program used to list all files in a directory, starting with the name ls will generate a simple list of files, and starting with the name l , will generate a multi-column simple list, if it is started with the name ll, it will generate a detailed list of files, the program will check the first parameter to determine which name to start with, and select the start option according to the name.
2) Command line parameters There may be zero or more options after the program name, followed by zero or more file names, the option name starts with -, and the main program processes the parameters.

String constant
1, the string constant appears in the expression, its value is a pointer constant, the compiler stores a copy of these specified characters in a certain location in memory, and stores a pointer to the first character pointer.
1) When an array name is used in an expression, its value is also a pointer constant.

2. You can perform subscript reference, indirect access, and pointer arithmetic on pointer constants. These operators are equivalent to the operations on pointer constants when they act on string constants.
1) String constants + subscript references, indirect access, and pointer arithmetic expressions are legal.

To summarize
1, a pointer variable can point to another pointer variable if declared properly.
1) Pointers to pointers need to be initialized before use.
2) Perform double indirect access to the pointer variable to obtain the target object. Of course, more layers of indirect access are also allowed.

2. You can also create pointers to functions and arrays, and create arrays of pointers.
1) Implement callback functions using function pointers. The pointer to the callback function is passed as a parameter to another function, which uses this pointer to call the callback function. Using this technique, you can create general-purpose functions, and the callback function performs special-case work.
2) The transfer table uses an array of function pointers. The functions must have the same prototype. Select the pointer through the subscript, and then call the corresponding function through the pointer to ensure that the subscript value does not exceed the boundary.

3. If an execution environment implements command line parameters, the parameters are passed to the main function through two formal parameters.
1) argc represents the number of parameters.
2) argv points to a pointer sequence ending with a NULL pointer, and each pointer points to a character sequence list command line parameter, the first parameter is the program name, and the command line parameter can be obtained through argv indirect access operation.

4. The value of the string constant in the expression is a constant pointer pointing to the first character of the string. Like the array name, the string constant can be used with pointer expressions and subscript references.

Guess you like

Origin blog.csdn.net/mei_true/article/details/131395721
Recommended