结构作为函数参数:

  声明了一个结构就有了一种自定义的数据类型,这个数据类型和int、float、double一样,int等基本类型可以作为函数的参数,那么这种个自定义的结构类型也应该可以作为函数参数,比如:

int numberofDays(struct date d);函数numberofDays的参数就是一种结构变量。

  整个结构是可以作为参数的值传入函数的,这时是在函数内新建一个结构变量,并复制调用者的结构的值,这和数组是完全不一样的。结构除了可以作为参数,也可以作为返回值,例如下述程序中,直接将结构变量d传给另外两个函数:

 #include <stdio.h>
#include <stdbool.h> struct date{
int month;
int day;
int year;
}; bool isLeap(struct date d); //判断是否闰年 ,若是返回true,否则返回false
int numberofDays(struct date d); int main(int argc,char const *argv[]){ struct date today,tomorrow; //定义两个结构变量
printf("Enter today's date (mm dd yyyy):"); //输入今天的日期
scanf("%i %i %i",&today.month,&today.day,&today.year);//.运算符比&运算符优先级高 if(today.day != numberofDays(today)){ //如果today不是这个月的最后一天,那么明天就是今天加1,年和月不变
tomorrow.day=today.day +;
tomorrow.month=today.month;
tomorrow.year=today.year;
}else if(today.month == ){ //如果today是这个月的最后一天,且月份是12月份,那么第二天就是新的一年,月日都是1,年加1
tomorrow.day=;
tomorrow.month=;
tomorrow.year=today.year+;
}else{ //如果today不是这个月的最后一天,且月也不是12月,那么明天就是下个月,月加1,day是1,year不变。
tomorrow.day=;
tomorrow.month=tomorrow.month+;
tomorrow.year=today.year;
} printf("Tomorrow's date is %i-%i-%i.\n'",tomorrow.year,tomorrow.month,tomorrow.day); return ;
} int numberofDays(struct date d){
int days;
const int daysPerMonth[]={,,,,,,,,,,,};//每个月有多少天
if(d.month==&&isLeap(d)) //如果是2月且是闰年,days是29天
days=;
else
days=daysPerMonth[d.month-];//否则直接输出该月的天数,之所以减1是因为数组是从0开始下标的
return days; //单一出口
} bool isLeap(struct date d){
bool leap=false;
if((d.year%== && d.year%!=)||d.year%==)
leap =true;
return leap;
}

输入结构:

  int等基础类型可以%d直接使用scanf和printf进行输入输出,但没有一个直接的方式可以一次scanf一个结构,如果打算写一个函数来读入结构:

 #include <stdio.h>
struct point{
int x;
int y;
};
void getStruct(struct point); //做了一个函数getStruct,参数是一个结构
void output(struct point);
int main(void){
struct point y={,}; //定义一个结构变量y
getStruct(y); //将这个结构变量y给函数getStruct,
output(y);
return ;
} void getStruct(struct point p){ //作为参数接收到结构变量y的值 ,p是和y相同值的结构变量
scanf("%d",&p.x);
scanf("%d",&p.y); //在函数内给p的x和y做了赋值
printf("%d,%d\n",p.x,p.y); //赋值后输出出来
} void output(struct point p){
printf("%d,%d\n",p.x,p.y);
}

  编译和运行上述代码,并输入数据4 和5.结果如下:

4 5
4,5
0,0 --------------------------------
Process exited after 2.377 seconds with return value 0
请按任意键继续. . .

  在getStruct函数中输入4和5之后,该函数内部输出4和5,但是回到main中,y的值依然是0和0.在getStruct中的变量p只是得到y的值,p和y并没有联系。这一点和数组不一样,C语言里在函数调用时是传值的,所以在getStruct函数中的p与main中的y是不同的,在函数getStruct读入了p的数值之后,没有任何东西回到main,所以y还是{0,0}.

解决方案一 将结构变量作为函数返回值:

  所以,把一个结构传入了函数,然后在函数中操作,但是没有返回回去,问题在于传入函数的是外面那个结构的克隆体,而不是指针,传入结构和传入数组是不同的,那么怎么解决这个问题呢?一种方案是在这个输入函数中,完全可以创建一个临时的结构变量,然后把这个结构返回给调用者,也就似乎可以把上述程序改成这个样子:

 #include <stdio.h>
struct point{
int x;
int y;
}; struct point getStruct(void); //做了一个函数getStruct,没有参数,返回一个结构变量
void output(struct point);
int main(void){
struct point y={,};
y=getStruct(); //调用函数getStruct时,使用y接收该函数的返回值
output(y);
return ;
} struct point getStruct(void){
struct point p; //做一个本地结构变量p,这个变量在离开这个函数时会消失掉
scanf("%d",&p.x);
scanf("%d",&p.y);
printf("%d,%d\n",p.x,p.y);
return p;
} void output(struct point p){
printf("%d,%d\n",p.x,p.y);
}

  在输入函数getStruct返回一个结构变量,而没有传入参数,在函数内新建一个本地结构变量p,给p赋值,然后将p返回给main,结果如下,这时y的值就发生了变化:

4 5
4,5
0,0 --------------------------------
Process exited after 2.377 seconds with return value 0
请按任意键继续. . .

解决方案二 结构指针作为函数参数:

  在将一个结构传递给函数时,一般不传结构,而是传递结构的指针,这也是比较推荐的方式。在C语言的经典教材K & R说过(p.131):

If a large structure is to be passed to a function, it is generally more efficient to pass a pointer than to copy the whole structure.

  因为在C语言中结构的传递方式是值的传递,也就是说需要在被调用的函数里建立一个和调用函数里一模一样的一个结构变量,然后把每一个变量成员的值都拷贝过去,这是一个即费空间(需要建立一个空间存放新的结构变量)又费时间(需要花费时间拷贝)的事情。所以早在K&R的书中就说过,更好的方式是传指针。

  因为结构变量名字不是地址,所以需要使用&符号取得地址赋值给一个结构指针,那么使用结构指针怎么获取变量的成员?有两种方式,一般使用->箭头指向的方式:

struct date{
int month;
int day;
int year;
} myday; struct date *p = &myday; (*p).month =12; //*p就表示myday,但是这种书写起来麻烦
p->month =12; //一般使用这种,一般说成p所指的month成员

  所有有了这种方式,getStruct就可以改造成下面的样子:

 #include <stdio.h>
struct point{
int x;
int y;
};
struct point* getStruct(struct point*); //做了一个函数getStruct,参数是一个结构
void output(struct point);
void print(const struct point *p);
int main(int argc, char const *argv[]){
struct point y={,}; //定义一个结构变量y
getStruct(&y); //将这个结构变量y给函数getStruct,
output(y);
output(*getStruct(&y)); //对getStruct函数的返回值进行*运算符,取出函数返回值的结构地址里的值。
print(getStruct(&y)); //将getStruct函数的返回值返回给print,打印出来 getStruct(&y)->x =; //甚至可以对返回值赋值
*getStruct(&y)=(struct point){,};
return ;
} struct point * getStruct(struct point *p){ //作为参数接收到结构变量y的值 ,p是和y相同值的结构变量
scanf("%d",&p->x);
scanf("%d",&p->y); //在函数内给p的x和y做了赋值
printf("%d,%d\n",p->x,p->y); //赋值后输出出来
return (struct point *)p; //常用套路, 传进函数一个指针,对该指针做了一定处理后,再传出去 。好处是可将该返回值潜在其他函数里。
} void output(struct point p){
printf("%d,%d\n",p.x,p.x);
}
void print(const struct point *p){ //传递一个point指针,只是输出所以const,不修改point
printf("%d,%d",p->x,p->y);
}

最新文章

  1. linux ‘|’ 与重定向 实例详解
  2. Android中的动画效果
  3. &lt;2016-1-29&gt;
  4. CentOS 6.2编译安装Nginx1.2.0+MySQL5.5.25+PHP5.3.13
  5. ios app 实现热更新(无需发新版本实现app添加新功能)
  6. feature.shape和feature.shapecopy的区别
  7. Java—Integer类
  8. lhgDialog
  9. 1_HelloWorld
  10. Amazon Hiring Campus 2013 - Final 6
  11. Effective C++ -- 构造析构赋值运算
  12. HDU 2514 Another Eight Puzzle(DFS)
  13. 指路Reactive Programming
  14. C#参考教程 http://www.csref.cn
  15. centos安装系统全过程
  16. 阿里云申请免费https证书 + IIS服务器安装
  17. JS模板引擎handlebars.js的简单使用
  18. JS 使用html2canvas实现截图功能的问题记录和解决方案
  19. 【LeetCode题解】232_用栈实现队列(Implement-Queue-using-Stacks)
  20. in文件

热门文章

  1. 如何登录mysql? cmd怎么连接mysql数据库||从MYSQL客户端登录MYSQL
  2. 关于js中 toFixed()的一个小坑
  3. windows下安装和配置nginx
  4. LightOJ1370 Bi-shoe and Phi-shoe
  5. 浅尝Code Map
  6. RBM如何训练?
  7. 软件License认证方案的设计思路
  8. c# List实现原理
  9. UML 中extend和include的区别
  10. NGINX压力测试