结构体指针作为函数参数

 1 #include <stdio.h>//原来有时候结构体变量不适宜在main函数外申明
 2  
 3 struct date_rec
 4 {
 5     int day;
 6     int month;
 7     int year;
 8 };
 9  
10 struct date_rec current_date;
11  
12 int days_of_month[][13] = {{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
13 };
14  
15 int is_leap(int year)
16 {
17     return ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0)));
18 }
19  
20 void input_date(struct date_rec *current_date)
21 {
22     printf("请输入当前日期(年 月 日):");
23     scanf("%d%d%d", &current_date->year, &current_date->month, &current_date->day);
24 }
25  
26 void increment_date(struct date_rec *current_date)
27 {
28     current_date->day++;
29     if (current_date->day > days_of_month[is_leap(current_date->year)][current_date->month])
30     {
31         current_date->day = current_date->day - days_of_month[is_leap(current_date->year)][current_date->month]
32                             ;
33         current_date->month++;
34         if (current_date->month > 12)
35         {
36             current_date->year++;
37             current_date->month = current_date->month - 12;
38         }
39     }
40 }
41  
42 void output_date(struct date_rec *current_date)
43 {
44     printf("当前日期:%d年%d月%d日!", current_date->year,
45            current_date->month, current_date->day);
46 }
47  
48 int main()
49 {
50     input_date(&current_date);
51     increment_date(&current_date);
52     output_date(&current_date);
53  
54     return 0;
55 }

 

posted @ 2014-03-29 21:39  xzenith  阅读(942)  评论(0编辑  收藏  举报