这种返回指针值的函数,一般定义形式为
类型名 * 函数名 ( 参数列表 ) 例如 int *a( int x,int y);
例:有若干学生的成绩(每个学生有4门成绩),要求在用户在输入学生序号以后,能输出该学生的全部成绩。用指针函数来实现。
1
#include
<
stdio.h
>
2 #include < stdlib.h >
3
4 int main()
5 {
6 float score[][ 4 ] = {{ 60 , 70 , 80 , 90 },{ 56 , , 67 , 88 },{ 34 , 78 , 90 , 66 }};
7 float * search( float ( * pointer)[ 4 ], int n); // 函数声明
8 float * p;
9 int i,m;
10 printf( " enter the number of student: " );
11 scanf( " %d " , & m);
12 printf( " The score of No.%d are:\n " ,m);
13 p = search(score,m); // 函数调用
14 for (i = 0 ;i < 4 ;i ++ )
15 printf( " %5.2f\t " , * (p + i));
16 printf( " \n " );
17
18 return 0 ;
19 }
20
21 float * search( float ( * pointer)[ 4 ], int n)
22 {
23 float * pt;
24 pt = * (pointer + n);
25 return (pt);
26 }
2 #include < stdlib.h >
3
4 int main()
5 {
6 float score[][ 4 ] = {{ 60 , 70 , 80 , 90 },{ 56 , , 67 , 88 },{ 34 , 78 , 90 , 66 }};
7 float * search( float ( * pointer)[ 4 ], int n); // 函数声明
8 float * p;
9 int i,m;
10 printf( " enter the number of student: " );
11 scanf( " %d " , & m);
12 printf( " The score of No.%d are:\n " ,m);
13 p = search(score,m); // 函数调用
14 for (i = 0 ;i < 4 ;i ++ )
15 printf( " %5.2f\t " , * (p + i));
16 printf( " \n " );
17
18 return 0 ;
19 }
20
21 float * search( float ( * pointer)[ 4 ], int n)
22 {
23 float * pt;
24 pt = * (pointer + n);
25 return (pt);
26 }