可以在外部定义结构体类型,然后在主函数内部定义该类型的变量。在输入输出函数调用时,以结构体变量指针做为参数传递。
参考代码如下:
#include
struct test
{
int a;
};//定义结构体类型struct test。
void input(struct test* p)//输入函数,以指针作为参数。
{
scanf("%d",&p->a);
}
void output(struct test *p)//输出函数,以指针作为参数。这里也可以以结构体变量作为参数,不过用指针效率更高。
{
printf("%d\n", p->a);
}
int main()
{
struct test v;//定义结构体变量v。
input(&v);//输入。
output(&v);//输出。
return 0;
}
typedef struct student
{
char name;
int id
}stu;
int main(){
stu a;这就是结构体变量定义在主函数内
}
不懂继续追问
//我不知道你那个结构体中long num[5];和int s[4];分别表示什么,我把long num[5];改为了char num[5];我把它当学号处理的,int s[4];我是当作这个人的四科成绩,
//程序如下,看看不是,如不是,就把问题写明白,
#include
#define N 3//N个人,
struct stud{
char num[5];
char name[10];
int s[4];
double ave;
};
readrec(struct stud std[])
{
int i,k,sum;
for(i=0;i
sum=0;
printf("please input %d information.\n",i+1);
scanf("%s%s",std[i].num,std[i].name);
for(k=0;k<4;k++)
{scanf("%d",&std[i].s[k]);sum+=std[i].s[k];}
std[i].ave=(float)sum/4;
}
}
writerec(struct stud std[])
{
int i;
printf("the ave of the students is:\n");
for(i=0;i
}
void main()
{
struct stud student[N];
readrec(student);
writerec(student);
}