Digress
Today, I listened to lecture 5 of the "programming paradigm" and continued the topic of the previous article. I used the C language to implement simple generic programming.
Question
Recall the last function in the previous article:
Void * lsearch (void * Key, void * base, int N, int elemsize, INT (* cmpfn) (void *, void *)) {<br/> for (INT I = 0; I <n; ++ I) {<br/> void * elemaddr = (char *) base + I * elemsize; <br/> If (cmpfn (Key, elemaddr) = 0) <br/> return elemaddr; <br/>}< br/> return NULL; <br/>}
Define another function to be called:
Int intcmp (void * elem1, void * elem2) {<br/> int * IP1 = elem1; <br/> int * ip2 = elem2; <br/> return * IP1-* ip2; <br/>}
See the following call:
Int array [] = {1, 2, 4, 5, 6}; <br/> int size = 6; <br/> int number = 3; <br/> int * found = lsearch (& number, array, size, sizeof (INT), intcmp); <br/> If (found = NULL) <br/> printf ("-_-|"); <br/> else <br/> printf ("^_^ ");
If it is a C-style string, for example:
Char * Names [] = {"Marvin", "Marcus", "skyline", "Forward" };< br/> char * favorname = "skyline "; <br/> char ** found = lsearch (& favorname, names, 4, sizeof (char *), strcmp);/* The strcmp function is defined below. Note that the string. h contains the strcmp () function */<br/>
Note that the names array is an array that saves the pointer to the string's first address. The strings are stored in the text constant area.
Memory partition in C language:
STACK: the stack is automatically allocated and released by the compiler, and stores function parameter values and local variable values;
Heap: generally allocated and released by programmers. If not released, it may be recycled by OS;
Global (static): global variables and static variables are stored here. It is initialized in one block, but not initialized in another adjacent block. The program is released by the system.
Text Constant Area: stores constants and strings here. Released by the system.
Code area: stores the binary code of the function body.
Note that the names array stores the address of each string, so found is a second-level pointer to get the string. Note that & before favorname in the lsearch parameter table cannot be lost.
Strcmp () function definition:
Int strcmp (void * VP1, void * VP2.) {<br/> char * S1 = * (char **) VP1; // attention! <Br/> char * S2 = * (char *) VP2. // attention! <Br/> return strcmp (S1, S2); <br/>}
Pay attention to the second and third line. On the surface, the C language is a language with a strong type but a weak type. If no * (char **) exists, the system does not know the type of pointer that the CIDR Block and CIDR block point to. The compilation system reports the following error: void value not ignored as it ought to be. the lsearch function will pass in a pointer pointing to the first address of the string, that is, a second pointer, that is, char ** type, and unreference it to get a pointer pointing to the first address of the target string, the S1 pointer is assigned to S2. Then return the result of comparing two strings S1 and S2 by calling the system function strcmp.