C语言结构类型变量的说明
时间:2014-08-18 21:42 点击:次
说明结构变量有以下三种方法。以上面定义的stu为例来加以说明。
1) 先定义结构,再说明结构变量。
如:
- struct stu{
- int num;
- char name[20];
- char sex;
- float score;
- };
- struct stu boy1,boy2;
struct stu{
int num;
char name[20];
char sex;
float score;
};
struct stu boy1,boy2;
也可以用宏定义使一个符号常量来表示一个结构类型。例如:
- #define STU struct stu
- STU{
- int num;
- char name[20];
- char sex;
- float score;
- };
- STU boy1,boy2;
#define STU struct stu
STU{
int num;
char name[20];
char sex;
float score;
};
STU boy1,boy2;
2) 在定义结构类型的同时说明结构变量。
例如:
- struct stu{
- int num;
- char name[20];
- char sex;
- float score;
- }boy1,boy2;
struct stu{
int num;
char name[20];
char sex;
float score;
}boy1,boy2;
这种形式的说明的一般形式为:
struct 结构名{
成员表列
}变量名表列;
3) 直接说明结构变量。
例如:
- struct{
- int num;
- char name[20];
- char sex;
- float score;
- }boy1,boy2;
struct{
int num;
char name[20];
char sex;
float score;
}boy1,boy2;
这种形式的说明的一般形式为:
struct{
成员表列
}变量名表列;
第三种方法与第二种方法的区别在于第三种方法中省去了结构名,而直接给出结构变量。三种方法中说明的boy1、boy2变量都具有下图所示的结构。

说明了boy1、boy2变量为stu类型后,即可向这两个变量中的各个成员赋值。在上述stu结构定义中,所有的成员都是基本数据类型或数组类型。成员也可以又是一个结构,即构成了嵌套的结构。例如,下图给出了另一个数据结构。

按图可给出以下结构定义:
- struct date{
- int month;
- int day;
- int year;
- };
- struct{
- int num;
- char name[20];
- char sex;
- struct date birthday;
- float score;
- }boy1,boy2;
struct date{
int month;
int day;
int year;
};
struct{
int num;
char name[20];
char sex;
struct date birthday;
float score;
}boy1,boy2;
顶一下
(3)
100%
踩一下
(0)
0%
上一篇:C语言结构体的定义
下一篇:C语言结构变量成员的表示方法
相关内容:
QQ群
-
返回首页 -
返回顶部



