结构体
struct 结构体名
{
};
#include <stdio.h>
struct student
{
char name[20];
int age;
char sex;
int id;
};
int main()
{
struct student t;
scanf("%s%d %c%d",t.name, &t.age, &t.sex, &t.id);
printf("%s%d %c%d",t.name, t.age, t.sex, t.id);
return 0;
}
结构体指针数组变量:
#include <stdio.h>
struct student
{
char name[20];
int age;
char sex;
int id;
};
int main()
{
struct student *str[5];
int i;
for(i = 0; i < 5; i++)
{
str[i] = (struct student *)malloc(sizeof(struct student));
scanf("%s%d %c%d",str[i]->name, &str[i]->age, &str[i]->sex, &str[i]->id);
}
printf("******************************************\n");
for(i = 0; i < 5; i++)
{
printf("%s%d %c%d",str->name, str->age, str->sex, str->id);
}
return 0;
}
结构体长度://1、结构体总长度 一定是最长成员的整数倍(double除外)
//2、每个成员的位移量一定是该成员长度的整数倍
堆和栈的区别:
栈:操作系统管理,申请和释放都是操作系统完成
堆:用户管理,申请和释放由用户完成,申请malloc释放free
联合体
union test //所有成员共享同一段内存(只为最长成员分配空间)
{
};