C语言在linux下编译通过,一运行就段错误 200
#include"stdafx.h"#include<stdio.h>#include<stdlib.h>#include"stuscore.h"#defineN3str...
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include "stuscore.h"
#define N 3
struct student data[N];
void get(struct student *d,int n);
void calculate(struct student *d,int n);
void output(struct student *d,int n);
int main()
{
struct student *d;
d=(struct student*)malloc(sizeof(*d)*N);
get(data,N);
calculate(data,N);
output(data,N);
}
void get(struct student *d,int n)
{
int i;
FILE *r=fopen("students.txt","r");
for(i=0;i<n;i++)
{
fscanf(r,"%s%f%f%f%f",&d[i].name,&d[i].math,&d[i].ch,&d[i].en,&d[i].phy);
}
fclose(r);
}
void calculate(struct student *d,int n)
{
int i;
for(i=0;i<n;i++)
{
d[i].sum=d[i].math+d[i].ch+d[i].en+d[i].phy;
d[i].ave=d[i].sum/4;
}
}
void output(struct student *d,int n)
{
int i;
printf("name match chinese english physical sum ave\n");
for(i=0;i<n;i++)
{
printf("%s %.2f %.2f %.2f %.2f %.2f %.2f\n",d[i].name,d[i].math,d[i].ch,d[i].en,d[i].phy,d[i].sum,d[i].ave);
}
}
该代码在windows下vs能正常运行,但是学linux用gcc编译后,一运行就段错误 展开
#include <stdio.h>
#include <stdlib.h>
#include "stuscore.h"
#define N 3
struct student data[N];
void get(struct student *d,int n);
void calculate(struct student *d,int n);
void output(struct student *d,int n);
int main()
{
struct student *d;
d=(struct student*)malloc(sizeof(*d)*N);
get(data,N);
calculate(data,N);
output(data,N);
}
void get(struct student *d,int n)
{
int i;
FILE *r=fopen("students.txt","r");
for(i=0;i<n;i++)
{
fscanf(r,"%s%f%f%f%f",&d[i].name,&d[i].math,&d[i].ch,&d[i].en,&d[i].phy);
}
fclose(r);
}
void calculate(struct student *d,int n)
{
int i;
for(i=0;i<n;i++)
{
d[i].sum=d[i].math+d[i].ch+d[i].en+d[i].phy;
d[i].ave=d[i].sum/4;
}
}
void output(struct student *d,int n)
{
int i;
printf("name match chinese english physical sum ave\n");
for(i=0;i<n;i++)
{
printf("%s %.2f %.2f %.2f %.2f %.2f %.2f\n",d[i].name,d[i].math,d[i].ch,d[i].en,d[i].phy,d[i].sum,d[i].ave);
}
}
该代码在windows下vs能正常运行,但是学linux用gcc编译后,一运行就段错误 展开
3个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询