Void and function pointers. Hash Tables. Void pointers
• C does not allow us to declare and use void variables.
• Void can be used only as return type or parameter of afunction.
• C allows void pointers
• Question: What are some scenarios where you want to pass voidpointers?
• Void pointers can be used to point to any data type
• Int X; void forward P = & X;/forward points to int forward/
• Define oat F; void then p = & F;/specify points to operate oat tables/
• Void pointers cannot be dereferenced. The pointers shouldalways be cast before dereferencing.
Void merge p; printf ("% d", interval P);/specify invalid merge/
Void then P; int then PX = (INT then) P; printf ("% d", then px);/then valid then/
The void in C is as simple as the "0" in mathematics, but its appearance is of great significance. The variable cannot be declared as void. I think it is because the variable is declared to allocate space in the memory. Since the space is allocated, it cannot be called void. But sometimes variables need to be expressed as NULL, so NULL is used to represent them. NULL is a macro, and its value is generally 0.
No matter what type of pointer, it has a "parent type", pointer. Therefore, the void pointer is also a pointer, pointing to a data type of "null. "Everything is empty", so any type of pointer can point to null. No specific type is used. Only the starting position is known, and the length of memory occupied cannot be known. Therefore, data cannot be obtained correctly. To obtain the data, you have to assign the type to the void pointer.
Function pointers
• In some programming languages, functions are specified rst classvariables (can be passed to functions, returned from functions etc .).
• In C, function itself is not a variable. But it is possible todeclare pointer to functions.
• Question: What are some scenarios where you want to passpointers to functions?
• Declaration examples:
• Int (partition fp) (int)/partition notice the () Partition/
• Int (batch fp) (void merge, void merge)
• Function pointers can be assigned, pass to and from functions, placed in arrays etc.
• Can assign to a function pointer:
Int (partition fp) (void partition, void partition) = strcmp_wrapper; or
Int (partition fp) (void partition, void partition) = & strcmp_wrapper;
• Can call from function pointer: (str1 and str2 are strings)
Int ret = fp (str1, str2); or
Int ret = (distinct fp) (str1, str2 );
A function cannot be assigned a value like a variable. This is also quite understandable. The function is used to reuse a group of operations, so as to reduce the workload of programmers and program volume, and prevent repeated invention of the wheel ". But in this way, it cannot be "changed" like a variable, that is, after a method is used in the program, it cannot be changed dynamically by the value assignment method. Although the method code cannot be assigned a value directly, we can obtain its pointer and call the method through the pointer. No matter what type of pointer, it is always a "Pointer", that is, the pointer can be stored in the memory at will, and will not have a great impact on the size of the program and will not affect the reuse of methods.
We can also see from the method pointer that the "type" of the method is determined by the elements. Include at least the return type and parameter type.
Function pointers are used because they require flexibility, that is, they are not sure which method to use. Therefore, there should be a large number of applications in the framework and class libraries.
Callbacks
De effecnition: Callback is a piece of executable code passed tofunctions. In C, callbacks are implemented by passing functionpointers.
Example:
Void qsort (void serial arr, int num, int size, int (Serial fp) (void serial pa, void serial pb ))
• Qsort () function from the standard library can be sort anarray of any datatype.
• Question: How does it do that? Callbacks.
• Qsort () calla function whenever a comparison needs to bedone.
• The function takes two arguments and returns (<0,> 0) depending on the relative order of the two items.
A callback function is a set of executable code that is passed to the method. It is a command stream and data stream, not just data. In functional programming languages, callback functions are everywhere, because the operating mechanism is to constantly solve functions, and the functions in them can be assigned values like variables.
Hash table
Hash tables (hashmaps) combine linked list and arrays to providean ef extends cient data structure for storing dynamic data. Hash tablesare commonly implemented as an array of linked lists (hash tableswith chaining ).
A hash table maps a sparse data distribution to a dense distribution, and achieves or approaches the Random storage speed through the ing function. The hash efficiency depends on the ing function.