C语言程序设计(第三版).ppt
文本预览下载声明
printf(″max=″); process(a,b,max); printf(″min=″); process(a,b,min); printf(″sum=″); process(a,b,add); } int max(int x,int y) /* 函数定义 */ {int z; if(x>y)z=x; else z=y; return(z); } int min(int x,int y) /* 函数定义 */ {int z; if(x<y)z=x; else z=y; return(z); } int add(int x,int y) /* 函数定义 */ {int z; z=x+y; return(z); } void process(int x,int y,int (*fun)(int,int)) {int result; result=(*fun)(x,y); printf(″%d\n″,result); } 10.6 返回指针值的函数 一个函数可以带回一个整型值、字符值、实型值等,也可以带回指针型的数据,即地址。其概念与以前类似,只是带回的值的类型是指针类型而已。 这种带回指针值的函数,一般定义形式为 类型名 *函数名(参数表列); 例如: int *a(int x,int y); 例10.24 有若干个学生的成绩(每个学生有4门课程),要求在用户输入学生序号以后,能输出该学生的全部成绩。用指针函数来实现。 #include stdio.h void main() {float *score[ ][4]={{60,70,80,90}, {56,89,67,88},{34,78,90,66}}; float*search(float (*pointer)[4],int n); float*p; int i,m; printf(″enter the number of student:″); scanf(″%d″,&m); printf(″The scores of No.%d are:\n″,m); p=search(score,m); for(i=0;i<4;i++= printf(″%5.2f\t″,*(p+i)); } float * search(float (*pointer)[4],int n) { float *pt; pt=*(pointer+n); return(pt); } 运行情况如下: enter the number of student:1↙ The scores of No. 1 are: 56.00 89.00 67.00 88.00 例10.25 对上例中的学生,找出其中有不及格课程的学生及其学生号。 #include stdio.h void main() {float score[ ][4]={{60,70,80,90},{56, 89,67,88},{34,78,90,66}}; float search(float (*pointer)[4]); float*p; int i,j; for(i=0;i<3;i++) {p=search(score+i); if(p==*(score+i)) {printf(″No.%d scores:″,i); for(j=0;j<4;j++) printf(″%5.2f″,*(p+j)); printf(″\n″);} } } 10.7指针数组和指向指针的指针 10.7.1 指针数组的概念 一个数组,若其元素均为指针类型数据,称为指针数组,也就是说,指针数组中的每一个元素都相当于一个指针变量。一维指针数组的定义形式为 类型名数组名[数组长度]; 例如: int*p[4]; 例10.26 将若干字符串按字母顺序(由小到大)输出。 #include stdio.h #include string.h void main() {void sort(char *name[ ],int n); void printf(char *name[ ],int n); char *name[ ]={Follow me,BASIC,Great
显示全部