Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- int sortof(const void* a, const void* b);
- // const 限制變數不能再賦值。 void 未定型態的指標。
- int sortof(const void* a, const void* b){
- int *pa = (int* )a;
- int *pb = (int* )b;
- // (型態名稱)變數名稱 (int *)a 把 a 轉型 (cast) 成 int * 的型態。
- if(*pa < *pb) return 1;
- else if(*pa == *pb) return 0;
- else return -1;
- }
- int main(){
- int arr[10] = {13, 20, 50, 32, 12, 63, 60, 44, 91, 3};
- qsort( (void *) arr, 10, sizeof(int), sortof);
- int i = 0;
- for(i; i < 10; i++)
- printf("%d ", arr[i] );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement