struct 结构体名 { 类型标识符 成员名; 类型标识符 成员名; // 结构体类型定义得时候, //不可以赋初值。 }; // 不要忘记结尾得分号
结构体得声明:
struct stu { int num; char name [30]; };
stu是结构体类型,类型不分配内存,不能赋值,不能运算,不能存取。
struct stu { int num ; char name[30];} st1;
st1 是结构体变量分配内存,可以赋值,存储。
结构体变量定义得两种形式:
// 第壹种: 直接在类型后面定义struct stu{ int num; char name [20];} st1; // 不要忘记后面得分号 // 第二种形式: void main () { struct stu st1 ; // 不要忘记声明类型 }
初始化结构体变量得三种形式:
//第壹种形式: struct stu { int num; char name[20] }st1={ 100 , "sda"}; // 第二种: struct stu st1={1001,"sda"}; //第三种: st1.num=100; st1.name="sdda"; // 错误 sprintf (st1.name,sdda"); // 因为字符串是常量,不能被直接赋值
结构体用大括号赋值,只有在创建并初始化得时候才可以。
定义匿名结构体变量: 唯一得方法:
struct { int num; char name [30];} st1;
结构体变量得引用:
结构体变量名 . 成员
可以将一个结构体变量赋值给另一个结构体变量。但是两个结构体变量类型必须是同一类型
struct stu { int num; char name[20]; struct chid { int age; char sex; };};//结构体内部再次定义一个结构体,但是没有定义实例得时候,//再次定义得结构体内部得变量,会被当作母体结构体变量成员。
struct stu { int num; char name[20]; struct chid { int age; char sex; } hha={....};}st1;
结构体数组:
定义:struct student{ int num; char name[];};struct student stu[] ={ {100,"aaa"}, {101,"asd"}, {102,"asd"} }; struct student stu[] ={ 100,"s",102,"sa",103,"sad"}; // 也可以挨个得赋值。 但是匿名结构体不可以挨个得赋值。
结构体指针:
一,定义: struct 结构体名 *结构体指针名 (:存放结构体变量得起始地址) 指向结构体变量数组得指针: struct students { int num; char name[20]; }; struct students stu[3] ={ {100,"aaa"}, {101,"asd"}, {102,"asd"} }; struct students stu *p= stu; 二,引用: 1. 用成员名引用: stu[0].num; stu[i].name 2. 指针引用: *p.num ; *(p+1) .name p->num ; (P+1)-> name 3. 指针循环: for (;p<stu+n;p++) { p->num; p->name; }






