C语言的问题解决下!
#include <stdio.h>
# define N 12
typedef struct
{ char num[10];
double s;
} STREC;
double fun (STREC *a,STREC *b, int *n)
{int i;
double sum=0.0,avae=0.0;
for(i=0;i<N;i++)
sum+=a[i].s;
avae=sum/N;
for(i=0;i<N;i++)
if(avae<=a[i].s)
{b[*n]=a[i];*n=*n+1;}
return avae;
}
void main()
{
FILE *wf;
STREC s[N]={{"GA05",85},{"GA03",76},{"GA02",69},{"GA04",85},
{"GA01",91},{"GA07",72},{"GA08",64},{"GA06",87},
{"GA09",60},{"GA11",79},{"GA12",73},{"GA10",90}};
STREC h[N],t;
int i,j,n;
double ave;
ave=fun(s,h,&n);
printf("The %d student data which is higher than %7.3f:\n",n,ave);
for(i=0; i<n; i++)
printf("%s %4.1f\n",h[i].num,h[i].s);
printf("\n");
for(i=0; i<n-1; i++)
for(j=i+1;j<n;j++)
if(h[i].s<h[j].s)
{t=h[i];h[i]=h[j];h[j]=t;} /*分数从高到低排列*/
/******************************/
wf=fopen("out.dat","w");
fprintf(wf, "%d %7.3f\n",n,ave);
for(i=0; i<n; i++)
fprintf(wf, "%s %4.1f ",h[i].num,h[i].s);
fclose(wf);
/*****************************/
}
0X004010d9 指令引用的 0X3346305c 内存。该内存不能为 written .
运行出现这个 是怎么回事啊~~~~
答案:#include <stdio.h>
# define N 12
typedef struct
{ char num[10];
double s;
} STREC;
double fun (STREC *a,STREC *b, int *n)
{int i;
double sum=0.0,avae=0.0;
for(i=0;i<N;i++)
sum+=a[i].s;
avae=sum/N;
for(i=0;i<N;i++)
if(avae<=a[i].s)
{b[*n]=a[i];*n=*n+1;}
return avae;
}
void main()
{
FILE *wf;
STREC s[N]={{"GA05",85},{"GA03",76},{"GA02",69},{"GA04",85},
{"GA01",91},{"GA07",72},{"GA08",64},{"GA06",87},
{"GA09",60},{"GA11",79},{"GA12",73},{"GA10",90}};
STREC h[N],t;
int i,j,n=0; //n应该赋值为0
double ave;
ave=fun(s,h,&n);
printf("The %d student data which is higher than %7.3f:\n",n,ave);
for(i=0; i<n; i++)
printf("%s %4.1f\n",h[i].num,h[i].s);
printf("\n");
for(i=0; i<n-1; i++)
for(j=i+1;j<n;j++)
if(h[i].s<h[j].s)
{t=h[i];h[i]=h[j];h[j]=t;} /*分数从高到低排列*/
/******************************/
wf=fopen("out.dat","w");
fprintf(wf, "%d %7.3f\n",n,ave);
for(i=0; i<n; i++)
fprintf(wf, "%s %4.1f ",h[i].num,h[i].s);
fclose(wf);
/*****************************/
}
int i,j,n;
n = N;//你的变量n没初始值...
double ave;ave=fun(s,h,&n);//这是调用的时候。。这里没错。。可以这样。。
double fun (STREC *a,STREC *b, int *n)
{
int i;
double sum=0.0,avae=0.0;
//可以在这里添加一句 *n = 0, 或者在mian中声明 n 的时候赋初值,n = 0,都可以。。
for(i=0;i<N;i++)
sum+=a[i].s;
avae=sum/N;
for(i=0;i<N;i++)
if(avae<=a[i].s)
{b[*n]=a[i];*n=*n+1;}//问题出在这里,*n,也就是main中的 n,没赋初值就使用,这就不可以了。。
return avae;
}
上一个:C语言好吗??
下一个:C语言32关键字详解