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 较复杂指针说明
指向函数的指针_变量,可以放入数组、也传递到其他函数、或者由函数返回等。
/*
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 较复杂指针说明
相关文章推荐
- c++ 成员指针函数 实现委托----跨平台实现(复杂)
- C语言 复杂指针的申明问题 数组指针 指针数组 函数指针 指针函数一览无遗! C/C++求职面试必备考点(四)
- typedef使用大全3(指向函数的指针)//typedef的使用可以隐藏复杂的函数指针语法
- 函数传参、传引用、传指针时间复杂度分析
- 复杂的函数指针
- 认清多层复杂函数指针
- 复杂指针的定义(含复杂函数指针)
- 理解复杂的C/C++声明 const, typedef , 函数指针
- 关于复杂指针数组,函数以及函数指针,数组思考
- 一个复杂的声明,包含数组指针,指针数组,函数指针和指针函数的区别
- 给一个字符串,有大小写字母,要求写一个函数把小写字母放在前面 大写字母放在后面,尽量使用最小空间,时间复杂度。(即用指针做)。 如:aAbBcCdD ---àabcdABCD
- 关于复杂指针数组,函数以及函数指针,数组思考
- 理解复杂的C/C++声明 const, typedef , 函数指针
- 成员函数指针——为什么那么复杂?
- 返回类型是函数指针的函数指针 类比复杂的数组声明
- 复杂函数指针
- 理解复杂的C/C++声明 const, typedef , 函数指针
- 右左法则----复杂指针解析(函数指针与数组指针)
- C语言 复杂指针的申明问题 数组指针 指针数组 函数指针 指针函数一览无遗! C/C++求职面试必备考点(四)
- typedef 复杂函数指针