可以在外部定义结构体类型,然后在主函数内部定义该类型的变量.在输入输出函数调用时,以结构体变量指针做为参数传递.
参考代码如下:
#include?stdio.h
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;
#include?stdio.h?
struct?complex?
{?
int?re;?
int?im;?
};?
void?add(struct?complex?a,?struct?complex?b,?struct?complex?*c)
c-re=a.re+b.re;
c-im=a.im+b.im;
void?minus(struct?complex?a,?struct?complex?b,?struct?complex?*c)
c-re=a.re-b.re;
c-im=a.im-b.im;
int?main()?
struct?complex?x,y,s,p;?
scanf("%d%d",x.re,x.im);?
scanf("%d%d",y.re,y.im);?
add(x,y,s);?
minus(x,y,p);
return?0;?
#include stdio.h
char *SubjectName[] = {"数学","物理","化学","生物","语文","外语","政治","历史","体育","美术","音乐"}; //科目名称列表 可调整顺序
#define STUDENTS_MAX 100 //最大可输入的学生数量 根据实际情况调整
Student Stu[STUDENTS_MAX]; //结构体数组 全局变量 虽可以不用全局的,要是你自己写了很多函数,都要访问它们,不如就让它们做全局变量的好
void main()
int total,i,j,k;
printf("请输入学生总数[1-%d]:",STUDENTS_MAX);
scanf("%d",total);
if(totalSTUDENTS_MAX){printf("超出设计总数了");exit(-1);}
else if(total1){printf("没有学生统计个屁呀"); exit(-1);}
printf("请输入学生成绩,格式:\n\t姓名 ");
for(j=0;jSUBJECT;j++) printf("%s ",SubjectName[j]);
printf("\n");
for(i=0;itotal;i++)
Stu[i].core[SUBJECT]=0.0;//平均分
fflush(stdin); //清输入缓冲区
scanf("%s %f",Stu[i].name,Stu[i].core[0]); //至少有一门科目的
for(j=1;jSUBJECT;j++) //其它科目 定义了多少就输入多少
scanf("%f",Stu[i].core[j]);
Stu[i].core[SUBJECT] += Stu[i].core[j];
Stu[i].core[SUBJECT] /= SUBJECT; //平均分 只是求了没有输出
printf("\n不及格情况统计:\n");
for(k=0,i=0;itotal;i++)
int flag = 0;
strcpy(putbuf,Stu[i].name); //记下名字
for(j=0;jSUBJECT;j++)
flag++;
strcat(putbuf," ");
strcat(putbuf,SubjectName[j]);//不及格的科目名称
strcat(putbuf,":");
if(flag)
printf("%s %d门不及格\n",putbuf,flag); //有不及格则输出
k++; //统计不及格人数
if(!k)
printf("没有不及格的学生");
else
printf("有不及格科目的学生个数:%d",k);
//常看到有要这类程序的,今天写一个,给大家参考,别天天上来要程序啊,仿照这个自己编吧
第一个明显不合理,第一行分配的空间有什么用呢?
在另外一个函数里面使用的源代码如下:
#include"stdio.h"
#include "conio.h" //-------添加这个头文件,因为getch()函数来自它,否则编译会有警告
struct student ? /*定义结构体*/
void data_in(struct student putin[]);
void data_out(struct student *p);
void data_pout(struct student *s1);
data_in(pers); ? ? ?/*调用指针输入函数*/
data_out(sp); ? ? /*调用指针输出函数*/
data_pout(sp); ? /*调用函数名输出函数*/
getch();
扩展资料
①.、函数是C语言的基本组成元素,当我们调用一个函数时,需要明确函数名和实参列表.实参列表中的参数可以是常量、变量、表达式或者空,并且各参数之间要使用英文逗号分隔开来.
#includestdio.h
#includestring.h
#includestdlib.h
struct telephone
char name[10];
};
void search(struct telephone b[], char *x, int n);
int main()
int i,n;
struct telephone b[100];
char nane[100];
for(i=0;;i++)
printf("Please input name:");
gets(b[i].name);
if(b[i].name[0]=='#')
break;
printf("Please input telephone:");
gets(b[i].telno);
n=i;
printf("Please input you want to find name:");
gets(nane);
search(b,nane[0],n);
return 0;
void search(struct telephone b[],char *x,int n)
int i;
int find=0;
for(i=0;in;i++)
if(strcmp(x,b[i].name)==0)
printf("the telephone is %s\n",b[i].telno);
find=1;
if(find==0)
printf("Not found!");
以上就是土嘎嘎小编为大家整理的定义结构体编写函数c语言相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!