您的位置:首页 > Web前端 > HTML5

CH5-4 指针 函数 复杂指针

2009-12-22 11:34 169 查看
5.11 指向函数的指针_变量

指向函数的指针_变量,可以放入数组、也传递到其他函数、或者由函数返回等。


/*


a) int a; //An integer


b) int *a; //A pointer to an integer


c) int **a; //A pointer to a pointer to an integer


d) int a[10]; //An array of 10 integers


e) int *a[10]; //An array of 10 pointers to integers


f) int (*a)[10]; //A pointer to an array of 10 integers


g) int (*a)(int); //A pointer to a function a that takes


an integer argument and returns an integer


h) int (*a[10])(int); //An array of 10 pointers to functions that


take an integer argument and return an integer


*/


#include <stdio.h>


#include <stdlib.h>


#include <string.h>


//指向函数指针有三个用途:调用函数、数组、做函数的参数。


char* strCompare(char *str1, char *str2){ //每一个函数都有一个入口地址


return (strcmp(str1, str2) > 0) ? str1 : str2;


}


char* strMaxLength(char *str1, char *str2){


return strlen(str1) > strlen(str2) ? str1 : str2;


}


int main(int argc, char** argv) {


char *str1 = "Hello Stone River", *str2 = "Hello World", *result1, *result2;


char* (*strMax)(void *, void *); //指向函数的指针变量


strMax = strCompare;


result1 = strMax(str1, str2);


strMax = strMaxLength;


result2 = strMax(str1, str2);


printf("result1 = %s, result2 = %s\n", result1, result2);


//指针数组,每个数组元素是一个指针变量,将指向一个函数


char* (*strMaxArray[2])(void *, void *);


strMaxArray[0] = strCompare;


strMaxArray[1] = strMaxLength;


printf("result1 = %s, result2 = %s\n",


strMaxArray[0](str1, str2), strMaxArray[1](str1, str2));


return (EXIT_SUCCESS);


}


#include <stdio.h>


#include <stdlib.h>


#include <string.h>




#define MAXLINES 500


char *linePT[MAXLINES];




int readLines(char *linePT[], int nLines);


int writeLines(char *linePT[], int nLines);




int numberCMP(char *str1, char *str2){


double value1, value2;


value1 = atof(str1); //把字符串转换为float类型


value2 = atof(str2);


if(value1 < value2) return -1;


else if(value1 > value2) return 1;


else return 0;


}




void swap(void *array[], int i, int j){


void *tmp;


tmp = array[i];


array[i] = array[j];


array[j] = tmp;


}




void quickSort(void *array[], int left, int right,


int (*compare)(void *, void *)){ //指向函数的指针做参数,以方便动态绑定


int i, last;


if(left >= right) return;


swap(array, left, (left + right)/2);


last = left;


for(i = left+1; i <= right; i++){


if((*compare)(array[i], array[left])) //通过指针调用函数


swap(array, ++last, i);


}


swap(array, left, last);


quickSort(array, left, last-1, compare); //迭代调用


quickSort(array, last+1, right, compare);//迭代调用


}




int main(int argc, char** argv) {


int nLines;


int ifNumericSort = 0; //1 if numeric sort


if(argc > 1 && strcmp(argv[1], "-n") == 0)


ifNumericSort = 0;


if(nLines = readLines(linePT, MAXLINES) >= 0){


quickSort((void **)linePT, 0, nLines-1, //通过函数向指向函数的指针传地址值


(int (*)(void *, void *))(ifNumericSort ? numberCMP : strcmp));


return 0; //指向函数的指针做其他函数的参数


}else{


printf("Input too big to sort\n");


return 1;


}


}

5.12 较复杂指针说明
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: