什么是结构体
视频锁定
{$ currentTime | date:'mm:ss' $}
{$ timeLeft | date:'mm:ss' $}
结构体是由若干个"成员"组成的,每一个成员要有成员名称和类型,成员可以是基本数据类型或者一个结构体。一旦定义结构体类型,使用它的方式和其他类型一样,可以声明这个结构体的对象、指针和数组。
下面我们先来定义一个描述歌曲的结构体:
struct song{
char title[64]; //歌名
char artist[32]; //歌手
int time; //播放时间
struct date published;
//出版日期,这也是一个结构体
};
上面的song
就是一个机构体,它包含有4个成员,成员分别有不同的数据类型,通过上面的例子我们能总结出定义结构的一般格式:
struct 结构体名称 {成员列表};
struct
是一个C语言的关键字,是定义结构体所必须的。[结构体名称]就是上面的song,有时候可以省略。成员列表必须用花括号{}
括起来,最后是分号。
在线练习
{$ activeFileHint $}