#include <stdio.h> //fopen函数的包
#include <stdlib.h> //exit()函数的包
#include <unistd.h>
//定义一个结构- 把struct Person当做一种数据类型
struct Person{
char *name;
int age;
int high;
};
int main()
{
//结构的实例化,声明结构变量,特别像java的对象实例化
struct Person gaocun = {
.name = "gaocun888",
.age = 23,
.high = 175
};
//操作成员变量,变量名称.属性名称
printf("%s\n", gaocun.name);
gaocun.name = "大王";
printf("%s\n", gaocun.name);
//创建一个结构数组
struct Person per_arr[6] = {
gaocun,
{
.name = "金蝉子",
.age = 23,
.high = 175
},
{
.name = "齐天大声",
.age = 22,
.high = 185
},
{
.name = "猪八戒",
.age = 25,
.high = 160
},
{
.name = "沙悟净",
.age = 35,
.high = 170
},
{
.name = "白骨真君",
.age = 1000,
.high = 190
}
};
for (int i = 0; i < 6; ++i)
{
printf("%d=====name:%s age:%d high:%d\n", i, per_arr[i].name, per_arr[i].age, per_arr[i].high);
}
//创建一个嵌套数组
struct Student {
int id;
char *name;
struct Person leader;
};
struct Student one = {
.id = 8,
.name = "高三八班",
.leader = { .name = "张华", .age = 35, .high = 175}
};
printf("%d===%s===%s===%d===%d\n", one.id, one.name, one.leader.name, one.leader.age, one.leader.high );
//结构指针
//声明结构指针
struct Student *stuPt;
//初始化结构指针
stuPt = &one;
printf("%p===%zdBytes\n", stuPt, sizeof(stuPt) );
//通过指针操作成员变量
printf("name:%s\n",stuPt->name );
printf("id:%d\n",stuPt->id );
printf(".leader.name:%s\n",stuPt->leader.name );
return 0;
}