菜鸟求救:我的程序怎么总益出?

advance1cpu 2007-01-25 01:06:25
我的程序很简单,就是用一个指针来读取一个3X4的数组中的数据,可在运行search函数时,总是报错 益出,而aver函数就没问题,大家看一下把!
main()
{float aver(float *p,int n);
float search(float (*p)[4],int n);
float score[3][4]={{67,65,70,60},{80,87,90,81},{90,99,100,98}};
aver(score[0],12);
search(score,n);
}
float aver(float *p,int n)
{float*p_end;
float sum=0,aver;
p_end=p+n-1;
for(;p<=p_end;p++)
{sum=sum+(*p);}
aver=sum/n;
printf("average is %f\n",aver);
}
float search(float (*p)[4],int n)
{int i;
printf("the score of No.%d is :",n);
for(i=0;i<4;i++)printf("%.2f",*(*(p+n)+i));
}
对了,我用的是TC
...全文
382 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
LeoDiao 2007-01-28
  • 打赏
  • 举报
回复
震惊了!!!
rularys 2007-01-27
  • 打赏
  • 举报
回复
float aver(float *p,int n);
float search(float (*p)[4],int n);
int
main()
{
int n=2;
float score[3][4]={{67,65,70,60},{80,87,90,81},{90,99,100,98}};
aver(score[0],12);
search(score,n);
return 0;
}
float aver(float *p,int n)
{
float*p_end;
float sum=0,aver;
p_end=p+n-1;
for(;p<=p_end;p++)
{
sum=sum+(*p);
}
aver=sum/n;
printf("average is %f\n",aver);
return 0;
}
float search(float (*p)[4],int n)
{
int i;
printf("the score of No.%d is :\n",n+1);
for(i=0;i<4;i++)
printf("%.2f\t",*(*(p+n)+i));
return 0;
}

我测试了,在你的代码中把n定义好以后,就通过了,没有你所说的溢出问题啊
advance1cpu 2007-01-26
  • 打赏
  • 举报
回复
LJC2000谢了, 您能告诉我我原来错那里了吗?小的愚钝阿
LJC2000a 2007-01-26
  • 打赏
  • 举报
回复
float aver(float *p,int n);
float search(float (*p)[4],int n);
int main(void)
{


float score[3][4]={{67,65,70,60},{80,87,90,81},{90,99,100,98}};
aver(score[0],12);
search(score,1);//这里原来是N,最好改成数字,
return 0;
}
float aver(float *p,int n)
{
float*p_end;
float sum=0,aver;
p_end=p+n-1;
for(;p<=p_end;p++)
{
sum=sum+(*p);
}
aver=sum/n;
printf("average is %f\n",aver);
return 0;
}
float search(float (*p)[4],int n)
{
int i;
printf("the score of No.%d is :",n);
for(i=0;i<4;i++)
{
printf("%.2f\n",*(*(p+n)+i));

}
return 0;
}
advance1cpu 2007-01-25
  • 打赏
  • 举报
回复
'n'我后来定义为2,可还是不行,总是说益出,不知怎么回事;
我在主函数中已经定义了子函数啊!应该可以这么用吧,以前也这么用过,没错啊!
Great6661 2007-01-25
  • 打赏
  • 举报
回复
mark 一下,回去看看.
LeoDiao 2007-01-25
  • 打赏
  • 举报
回复
/**
*test1_1.c 测试CSDN'S http://community.csdn.net/Expert/topic/5318/5318152.xml?temp=.515423
*Enviroment: Win xp sp2 + Dev-C++4.9.2.2 + --std c99
*/
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
float aver(float *p,int n);
float search(float (*p)[4],int n);
int n = 0; // n (0..2)
const float score[3][4]={{67,65,70,60},{80,87,90,81},{90,99,100,98}};
aver(score[0],12);
search(score,n);
system("PAUSE");
return EXIT_SUCCESS;
}
/**
*Warning: Bad design to use 1-d array for real 2-d array
*/
float aver(float *p,int n)
{
float*p_end;
float sum=0,aver;
p_end=p+n-1;
for(;p<=p_end;p++)
{
sum=sum+(*p);
}
aver=sum/n;
printf("average is %f\n",aver);
}
float search(float (*p)[4],int n)
{
int i;
printf("the score of No.%d is :",n);
for(i=0;i<4;i++)printf("%8.2f",*(*(p+n)+i));
}
Sco_field 2007-01-25
  • 打赏
  • 举报
回复
前人已回答了,搂住可以揭帖了
Jacky_Dai_ 2007-01-25
  • 打赏
  • 举报
回复
函数应该先定义再调用啊,你的主函数在而调用的子函数却在后面
可定有问题啊
jixingzhong 2007-01-25
  • 打赏
  • 举报
回复
search(score,n);

在该调用之前,
请先定义 n 并赋值 ~
iu_81 2007-01-25
  • 打赏
  • 举报
回复
此处n为0,1,2
fallinleave 2007-01-25
  • 打赏
  • 举报
回复
for(i=0;i<4;i++)printf("%.2f",*(*(p+n)+i));

这句话错了

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧