正在加载图片...
3.打开c:lc-programming8pg8I03.cpp,要求从键盘输入某个学生的学号、姓名、出生年 月日,然后将结果输出。该程序使用结构体嵌套,请检查该程序的错误并修改,使程序能正 确运行。修改后的程序以pg8103ok.cpp保存。该源程序内容如下: #include <stdio.h> struct student /体学生信息/ { int no; 体学号*/ char name[10]; 体姓名*/ struct date birthday; /*日期*/ : struct date /体日期信息 { int month: /体月*制 int day; *日*1 int year, /*年*/ void main (void struct student s; printf("请输入学号、姓名、出生日期(年月日)n")方 scanf"%d%s%d%d%d" &s.no,s.name,&birthday.year,&birthday.month,&birthday.day); printf("学号:%d,姓名:%s,出生日期:%5d%4d%4d小n", s.no,s.name,birthday.year,birthday.month,birthday.day); 了提示:如何发现问题? 结构体可以嵌套使用,但要注意:在定义嵌套的结构类型时,必须先定义成员的结构类 型,再定义主结构类型。即:如果一个结构类型被引用于另一个结构类型中时,就必须在引 用的结构类型前事先定义,否则无法实现嵌套。这与在函数调用时必须事先声明或定义是同 -143-3.打开 c:\c-programming\8\pg8103.cpp,要求从键盘输入某个学生的学号、姓名、出生年 月日,然后将结果输出。该程序使用结构体嵌套,请检查该程序的错误并修改,使程序能正 确运行。修改后的程序以 pg8103_ok.cpp 保存。该源程序内容如下: #include <stdio.h> struct student /* 学生信息 */ { int no; /* 学号 */ char name[10]; /* 姓名 */ struct date birthday; /* 日期 */ }; struct date /* 日期信息 { int month; /* 月 */ int day; /* 日 */ int year; /* 年 */ }; void main (void ) { struct student s; printf("请输入学号、姓名、出生日期(年月日)\n"); scanf("%d%s%d%d%d", &s.no,s.name,&birthday.year, &birthday.month,&birthday.day); printf("学号: %d, 姓名:%s, 出生日期:%5d%4d%4d\n", s.no,s.name,birthday.year,birthday.month,birthday.day); } 提示:如何发现问题? 结构体可以嵌套使用,但要注意:在定义嵌套的结构类型时,必须先定义成员的结构类 型,再定义主结构类型。即:如果一个结构类型被引用于另一个结构类型中时,就必须在引 用的结构类型前事先定义,否则无法实现嵌套。这与在函数调用时必须事先声明或定义是同 - 143 -
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有