The original function is The C programint language 5.11 program for sorting The row, as follows:
void qsort(void *v[], int left, int right, int (*comp)(void *,void *)) { int i,last; if( left >= right) return; swap(v,left,(left+right)/2); last = left; for(i = left + 1; i <= right; ++i) if(comp(v[i],v[left]) < 0) swap(v,++last,i); swap(v,left,last); qqsort(v,left,last-1,comp); qqsort(v,last+1,right,comp); } void qsort(void *v[], int left, int right, int (*comp)(void *,void *)){ int i,last; if( left >= right) return; swap(v,left,(left+right)/2); last = left; for(i = left + 1; i <= right; ++i) if(comp(v[i],v[left]) < 0) swap(v,++last,i); swap(v,left,last); qqsort(v,left,last-1,comp); qqsort(v,last+1,right,comp);}
This function has the following features:
1. the void * v [] parameter is used to convert the real parameters passed by the main function into common void ** type parameters. Therefore, you must convert the real parameters by yourself, for example, if you want to pass char * lineptr [] to v, you must (void **) lineptr,
Then why is this conversion?
1. Any type can be assigned to the corresponding void type
2. The void type can also be forcibly converted to this type.
So why is this conversion?
1. in this way, the conversion can meet the universal requirements of the function. Any type can be converted to the corresponding void type. In contrast to this program, char is converted to void, as long as comp (char *, char *) is provided for calling in the comp model, void is converted to the char type for calculation, which meets the general requirements of the program;
2. the above void pointer can be used because all types of address bytes are fixed. win32 uses four bytes to indicate the pointer, therefore, as long as the void type operation in this function can all be performed on the address operation, the corresponding void type cannot be operated by four arithmetic operations and value calculation, although it points to the first address of the corresponding variable, but he does not know the Data Type pointed to by this address, so no other operations except the address operation can be performed, that is, using this conversion function, you must ensure that the function cannot perform other operations except the address operation.