结构体

原文链接:
http://www.dongeasy.com/software-development/embedded-system/1710.html

基本定义:结构体,通俗讲就像是打包封装,把一些有共同特征(比如同属于某一类事物的属性,往往是某种业务相关属性的聚合)的变量封装在内部,通过一定方法访问修改内部变量。

结构体定义:

第一种:只有结构体定义

struct stuff{
   char job[20];
   int age;
   float height;
};

第二种:附加该结构体类型的“结构体变量”的初始化的结构体定义

struct stuff{
   char job[20];
   int age;
   float height;
}DongEasy;

也许初期看不习惯容易困惑,其实这就相当于:

struct stuff{
   char job[20];
   int age;
   float height;
};
struct stuff DongEasy;

第三种:如果该结构体你只用一个变量DongEasy,而不再需要用

struct stuff yourname;

去定义第二个变量。

那么,附加变量初始化的结构体定义还可进一步简化出第三种:

struct{
   char job[20];
   int age;
   float height;
}DongEasy;

把结构体名称去掉,这样更简洁,不过也不能定义其他同结构体变量了

结构体变量及其内部成员变量的定义及访问:

就像刚才的第二种提到的,结构体变量的声明可以用:

struct stuff yourname;

其成员变量的定义可以随声明进行:

struct stuff DongEasy = {"manager",30,185};

也可以考虑结构体之间的赋值:

struct stuff faker = DongEasy; //或    struct stuff faker2; //      faker2 = faker;

结构体成员变量的访问除了可以借助符号”.”,还可以用”->”访问

DongEasy.job[0 = 'M';
DongEasy.job[1 = 'a';
DongEasy.age = 27;
DongEasy.height = 185;

猜你喜欢

转载自blog.csdn.net/gogomusic/article/details/79427071