结构体spa
为何须要结构体?.net
为了表示一些复杂的事务,而普通的基本类型变量没法知足实际要求指针
什么叫结构体?code
把一些基本类型数据结合在一块儿造成一个新的符合数据类型,这个叫作结构体orm
如何定义结构体?blog
有三种方式:事务
(第一种方式是值得推荐的)ci
struct student { int age; float score; int sex; };
第二种,能够直接写上变量名称(不经常使用)get
struct student1 { int age; float score; int sex; }allen;
第三种,能够省略掉一个类型名称,加上一个变量名(这种不经常使用)博客
struct { int age; float score; int sex; }frank;
怎么去使用结构体变量?
赋值和初始化
struct student member = {24, 66.6, 1};
如何取出结构体变量中的每个成员
1,结构体变量名.成员名
2,指针变量名->成员名 (第二种方式更经常使用)
1 pst->age 计算机内部会被转化成 (*pst).age 没有道理可讲,就这规定。
2 因此, pst->age 等价于 (*pst).age 也等价与 st.age
3 之因此知道 pst->age 等价于 st.age,是由于 pst -》age 是被转化成了(*pst).age来执行。
4 pst->age 的含义:pst所指向的那个结构体变量中的age这个成员
小案例:
# include <stdio.h> struct student { int age; float score; int sex; };//这里的分号是不能够少的,第一次写的时候坑死了 struct student1 { int age; float score; int sex; }allen; struct { int age; float score; int sex; }frank; int main(void) { struct student member = {24, 66.6, 1}; //haha = {24 , 66.6, 0}; //这样写是错的,建立变量的时候能够这样赋值,建立以后,就须要单个赋值了 allen.age = 24; allen.score = 66.6; frank.sex = 1; printf("%d \n",member.age); printf("%f \n",allen.score); printf("%d \n",frank.sex); return 0; } /* VC6.0++上运行的结果: ===================================== 24 66.599998 //前面记录过,浮点型是不能很是准确的记录一个小数的。 1 ===================================== 总结: 定义的同时能够总体赋值 若是定义完以后,则只能单个赋值了 */
学PHP的小蚂蚁 博客 http://my.oschina.net/woshixiaomayi/blog