结构体变量是什么意思?

用一两个例子说明下,可以复制程序用来说明,只要讲清楚就采纳

结构体是一种新数据类型,属构造类型,它由若干类型各异的“成员”组成;描述这些“成员”可以使用任何基本数据类型,甚至是另外一种构造数据类型都行。
定义结构类型使用关键字“ struct”。

下边几行定义结构类型 person ,并用该类型定义结构变量 zhangshan 和结构数组 class_one[100] :
struct person
{
char name[20]; /* 定义姓名 */
char color[10]; /* 定义肤色 */
char sex[2]; /* 定义性别 */
int age; /* 定义年龄 */
}; /* 注意这里有分号 */
struct person zhangshan, class_one[100];
现在,再定义一个商品结构类型 goods ,设商品包含属性有:商品名、商品代码、厂商、单价、质量。把相同类型的成员定义在一行, goods 可定义如下:
struct goods
{
char goodsname[15], goodcode[15], companyname[30] ;
float price, weight ;
} ;
struct 是结构体的关键字, goods 是结构体名,花括号内的所有变量是这个结构体的成员。这种写法虽然节省了程序行,但降低了可读性,故不建议初学者这样写。
综上,结构体类型简称为结构类型,其定义格式为:
struct < 结构体名 >
{
类型 成员 1;
类型 成员 2;
……
};
二、结构体类型变量的定义
结构体变量简称为结构变量,它由结构类型定义,有三种定义方法。下面以定义结构类型 book 和结构变量 mybook 、 storybook 为例说明之。
1. 先定义结构类型,再定义结构变量。
struct book /* 定义结构体类型 */
{
char bookname[20];
float price;
char publisher[20];
char author[10];
} ;
struct book mybook, storybook;
用这种方法定义结构变量,是最常用的方法,但须注意不能省略关键字“ struct ”。还可以在定义结构变量的同时给它的成员赋初值。如:
struct book /* 定义结构体类型 */
{
char bookname[20];
float price;
char publisher[20];
char author[10];
} ;
struct book mybook = { “maths”, 24.7, “ 电子社 ”, “zhao” }, storybook;
则, mybook 变量的 price = 24.7 。
2. 定义结构类型的同时定义结构变量。
struct book /* 定义结构体类型 */
{
char bookname[20];
float price;
char publisher[20];
char author[10];
} struct book mybook, storybook;
3. 不定义结构类型,直接定义结构变量。
struct /* 不定义结构类型名 */
{
char bookname[20];
float price;
char publisher[20];
char author[10];
} struct book mybook, storybook;
需要说明的是,当某结构类型的成员又是另外一个结构类型时,称嵌套定义,其定义方法如下:
struct brith_date
{
int month ;
int day ;
int year ;
} ;
struct
{
char name[10] ;
char address[30];
char tel[12];
int age;
struct data birthday;
char sex[3];
} student_01 , employee ;
此例直接定义了 student_01 和 employee 两个变量,但是没有定义此结构体的名字,因此不能再定义与 student_01 和 employee 同类的其它结构变量了。
温馨提示:答案为网友推荐,仅供参考

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网