C语言学习(六)指针5 返回指针的函数

返回指针的函数

1.一个函数可以返回指针型的数据,即地址。其一般定义形式为:

类型名*函数名(参数列表);

例如:

int* max(int x,int y);

max是函数名,调用这个函数得到一个指向整形数据的指针。注意在*max两侧没有括号。()运算符高于*运算符,因此max先与()结合,再与*结合。


2.例子,有若干学生成绩,每个学生有4门课程。输入一个序列号,能输出该学生的全部成绩。用指针函数实现。


#include<stdio.h>

void main() {

float score[][4] = {{56,33,25,86},{87,54,3,65},{98,56,4,33},{53,2,45,23}};

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("%0.2f\t",*(p+i));

}

printf("\n");

}

float * search(float (* pointer)[4],int n) {

float * pt;

pt= *(pointer+n);

return(pt);

}


输出:

enterthe number of student:2

thescores of NO.2 are:

98.00 56.00 4.00 33.00


3.找出其中不及格的学生以及学生号


#include<stdio.h>

void main() {

int a[][4] = {{76,76,58,67},{88,79,87,69},{98,78,78,97}};

int * search(int (*p)[4]);

int * p;

int i,j;

for(i= 0; i < 3; i++) {

p= search(a+i);

if(p== *(a+i)) {

printf("No:%dscrores:",i);

for(j = 0; j < 4; j++)

printf("%d",*(p+j));

printf("\n");

}

}

}

int * search(int (*p)[4]) {

inti,*pt;

pt= *(p + 1); //pt指向本行末尾,用pt作为区分有无不及格课程的标志。若有,就让pt指向本行0列元素,否则指向本行末尾。

for(i= 0; i < 4; i++) {

if(*(*p+i)< 60)

pt= *p;

}

returnpt;

}

文章链接:http://blog.csdn.net/murongshusheng/article/details/8699899


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值