参考:c++primer(第六版)
结构体:封装了多种常用数据类型的一种数据类型(等于同int float等,不过int 这类的为c++定义,而结构体自己定义)
例如:
structinflatable
{
char name[20];
flaot volume;
};
struct为关键字 inflatable为数据类型的名称(等价与int)
创建struct变量
inflatable hat;
inflatable* hat;
inflatable hat[20];
(注意:c语言定义时必须有struct,struct inflatable hat,c++可以省略struct。访问结构体成员. hat.name)
结构体初始化
inflatable hat={"hat",2.2};
inflatable hat{"hat",2.2};
struct inflatable hat={"hat",2.2};
inflatable hat{};//每个成员赋值0;
inflatable hhat=hat;//即使结构体内有数组也可以直接赋值
(注意,c++中结构体类似class,区别,class默认是private,结构体为public ,与c的区别为:c++结构体可以添加函数)
特殊的例子
struct
{
char name[20];
flaot price
}hat;
定义了一个名叫hat的结构体,hat不能用来定义别的结构体,为一次性定义变量,不建议如此使用结构体
typedef struct sdudent
{
char name[20];
int age
}* stu;
stu表示一个指向结构体的指针类型 stu stu1表示一个结构体指针变量stu1 (类似typedef int * int_p int_p a等价于 int * a)