结构体
来源:互联网 发布:ubuntu怎么更新 编辑:程序博客网 时间:2024/05/18 13:12
一、一个变量按理占内存占( 4 + 20 + 1 + 4 + 4 + 30 = 63 )个字节,但考虑内存对齐,故sizeof(student1)往往不等于63
struct student
{int num;
char name[20];
char sex;
int age;
float score;
char addr[30];
} student1, student2
二、定义嵌套结构
void main(){ struct date { int month; int day; int year; }; struct { int num; char name[20]; char sex; struct date birthday; float score; } boy1, boy2;
三、定义结构体数组 struct student student[3]; 或
struct student{int num;char name[20];char sex;int age;float score;char addr[30]; }student[3];
四、结构体变量初始化
struct student /*定义结构*/ { int num; char *name; char sex; float score; }boy1, boy2 = { 102, "Jane", 'M', 98.5 };
struct student{int num;char name[20]; char sex; int age; float score; char addr[30];}stu[2]= { {101,″LiLin″,′M′,18,87.5,″Beijing″}, {102,″Zhang″,′F′,19,99,″Shanghai″} };
五、结构体指针
1、结构指针变量说明的一般形式为:struct 结构名 *结构指针变量名 如 struct stu *pstu;
0 0