目录
1 结构体变量作函数参数
结构体变量可以像普通变量一样作为函数的参数。其内部运行也是传值不传参,即进入从main函数进入新函数后,新函数会再在内存中创造一个和传进来的结构体一模一样的结构体(地址不同),新函数会在新产生的结构体基础之上对其进行操作,而不会改变原结构体。例如下面的程序:
#include<stdio.h>
struct adate
{
int year;
int month;
int day;
};
void get_today(struct adate today)
{
printf("请按顺序输入年月日:\n");
scanf("%d",&today.year);
scanf("%d",&today.month);
scanf("%d",&today.day);
printf("get_today函数中的today是:%d年%d月%d日\n",today.year,today.month,today.day);
}
void main(void)
{
struct adate today={0,0,0};
get_today(today);
printf("原today是:%d年%d月%d日\n",today.year,today.month,today.day);
}
运行结果为: