结构体
作用:
封装数据(把多种不同的数据类型放在一起)
注意:
一般放在全局;分号不能省略。
结构体变量用点访问
结构体指针用->访问
初始化:
静态初始化
动态初始化
使用注意事项:
给结构体中的数组成员赋值时,不能直接将字符串赋给数组名,可以使用strcpy函数
给结构体中的指针变量成员赋值时,要先给指针分配空间,否则它会使一个野指针
静态初始化即在定义的时候直接赋值
结构体数组的初始化:使用for循环来完成
结构体中的对齐方式:
字对齐和半字对齐
字对齐:给每个变量先分配四个字节的空间,如果下一个数据的数据类型与之相同,就可以放在一起,如果空间不够就再开辟四个字节的空间;如果类型不相同,就直接开辟四个字节的空间
半字对齐:原理和字对齐大致相同,但是分配的空间为2个字节
特别的:
char
short :8字节
char
char
char :4字节
short
内存空洞:由于字对齐和半字对齐造成的内存浪费
解决方法:在定义结构体的时候,尽量将相同类型的数据放在一起
共用体
和结构体的区别:
1.共用体的大小是其中最长的数据类型的长度,共用体中所有的成员共用同一段内存空间,但是其也满足字对齐和半字对齐
2.共用体共用同一段空间,造成数据覆盖
<a target=_blank id="L1" href="http://blog.youkuaiyun.com/qq_31108501/article/details/52032796#L1" rel="#L1" style="text-decoration: none; color: rgb(12, 137, 207);"> 1</a> <a target=_blank id="L2" href="http://blog.youkuaiyun.com/qq_31108501/article/details/52032796#L2" rel="#L2" style="text-decoration: none; color: rgb(12, 137, 207);"> 2</a> <a target=_blank id="L3" href="http://blog.youkuaiyun.com/qq_31108501/article/details/52032796#L3" rel="#L3" style="text-decoration: none; color: rgb(12, 137, 207);"> 3</a> <a target=_blank id="L4" href="http://blog.youkuaiyun.com/qq_31108501/article/details/52032796#L4" rel="#L4" style="text-decoration: none; color: rgb(12, 137, 207);"> 4</a> <a target=_blank id="L5" href="http://blog.youkuaiyun.com/qq_31108501/article/details/52032796#L5" rel="#L5" style="text-decoration: none; color: rgb(12, 137, 207);"> 5</a> <a target=_blank id="L6" href="http://blog.youkuaiyun.com/qq_31108501/article/details/52032796#L6" rel="#L6" style="text-decoration: none; color: rgb(12, 137, 207);"> 6</a> <a target=_blank id="L7" href="http://blog.youkuaiyun.com/qq_31108501/article/details/52032796#L7" rel="#L7" style="text-decoration: none; color: rgb(12, 137, 207);"> 7</a> <a target=_blank id="L8" href="http://blog.youkuaiyun.com/qq_31108501/article/details/52032796#L8" rel="#L8" style="text-decoration: none; color: rgb(12, 137, 207);"> 8</a> <a target=_blank id="L9" href="http://blog.youkuaiyun.com/qq_31108501/article/details/52032796#L9" rel="#L9" style="text-decoration: none; color: rgb(12, 137, 207);"> 9</a> <a target=_blank id="L10" href="http://blog.youkuaiyun.com/qq_31108501/article/details/52032796#L10" rel="#L10" style="text-decoration: none; color: rgb(12, 137, 207);"> 10</a> |
来自CODE的代码片
snippet_file_0.txt
运用:
一般使用共用体来验证CPU的大小端(大端字节序和小段字节序是CPU的属性,与操作系统无关):
小端字节:低字节存放在低地址,高字节存放在高地址
大端字节:高字节存放在低地址,低字节存放在高地址
无论大端字节还是小端字节,都是从低字节开始存放的
<a target=_blank id="L1" href="http://blog.youkuaiyun.com/qq_31108501/article/details/52032796#L1" rel="#L1" style="text-decoration: none; color: rgb(12, 137, 207);"> 1</a> <a target=_blank id="L2" href="http://blog.youkuaiyun.com/qq_31108501/article/details/52032796#L2" rel="#L2" style="text-decoration: none; color: rgb(12, 137, 207);"> 2</a> <a target=_blank id="L3" href="http://blog.youkuaiyun.com/qq_31108501/article/details/52032796#L3" rel="#L3" style="text-decoration: none; color: rgb(12, 137, 207);"> 3</a> <a target=_blank id="L4" href="http://blog.youkuaiyun.com/qq_31108501/article/details/52032796#L4" rel="#L4" style="text-decoration: none; color: rgb(12, 137, 207);"> 4</a> <a target=_blank id="L5" href="http://blog.youkuaiyun.com/qq_31108501/article/details/52032796#L5" rel="#L5" style="text-decoration: none; color: rgb(12, 137, 207);"> 5</a> <a target=_blank id="L6" href="http://blog.youkuaiyun.com/qq_31108501/article/details/52032796#L6" rel="#L6" style="text-decoration: none; color: rgb(12, 137, 207);"> 6</a> <a target=_blank id="L7" href="http://blog.youkuaiyun.com/qq_31108501/article/details/52032796#L7" rel="#L7" style="text-decoration: none; color: rgb(12, 137, 207);"> 7</a> <a target=_blank id="L8" href="http://blog.youkuaiyun.com/qq_31108501/article/details/52032796#L8" rel="#L8" style="text-decoration: none; color: rgb(12, 137, 207);"> 8</a> <a target=_blank id="L9" href="http://blog.youkuaiyun.com/qq_31108501/article/details/52032796#L9" rel="#L9" style="text-decoration: none; color: rgb(12, 137, 207);"> 9</a> <a target=_blank id="L10" href="http://blog.youkuaiyun.com/qq_31108501/article/details/52032796#L10" rel="#L10" style="text-decoration: none; color: rgb(12, 137, 207);"> 10</a> <a target=_blank id="L11" href="http://blog.youkuaiyun.com/qq_31108501/article/details/52032796#L11" rel="#L11" style="text-decoration: none; color: rgb(12, 137, 207);"> 11</a> <a target=_blank id="L12" href="http://blog.youkuaiyun.com/qq_31108501/article/details/52032796#L12" rel="#L12" style="text-decoration: none; color: rgb(12, 137, 207);"> 12</a> <a target=_blank id="L13" href="http://blog.youkuaiyun.com/qq_31108501/article/details/52032796#L13" rel="#L13" style="text-decoration: none; color: rgb(12, 137, 207);"> 13</a> <a target=_blank id="L14" href="http://blog.youkuaiyun.com/qq_31108501/article/details/52032796#L14" rel="#L14" style="text-decoration: none; color: rgb(12, 137, 207);"> 14</a> <a target=_blank id="L15" href="http://blog.youkuaiyun.com/qq_31108501/article/details/52032796#L15" rel="#L15" style="text-decoration: none; color: rgb(12, 137, 207);"> 15</a> <a target=_blank id="L16" href="http://blog.youkuaiyun.com/qq_31108501/article/details/52032796#L16" rel="#L16" style="text-decoration: none; color: rgb(12, 137, 207);"> 16</a> <a target=_blank id="L17" href="http://blog.youkuaiyun.com/qq_31108501/article/details/52032796#L17" rel="#L17" style="text-decoration: none; color: rgb(12, 137, 207);"> 17</a> <a target=_blank id="L18" href="http://blog.youkuaiyun.com/qq_31108501/article/details/52032796#L18" rel="#L18" style="text-decoration: none; color: rgb(12, 137, 207);"> 18</a> <a target=_blank id="L19" href="http://blog.youkuaiyun.com/qq_31108501/article/details/52032796#L19" rel="#L19" style="text-decoration: none; color: rgb(12, 137, 207);"> 19</a> <a target=_blank id="L20" href="http://blog.youkuaiyun.com/qq_31108501/article/details/52032796#L20" rel="#L20" style="text-decoration: none; color: rgb(12, 137, 207);"> 20</a> <a target=_blank id="L21" href="http://blog.youkuaiyun.com/qq_31108501/article/details/52032796#L21" rel="#L21" style="text-decoration: none; color: rgb(12, 137, 207);"> 21</a> |
来自CODE的代码片
snippet_file_0.txt
枚举
只能代替整数宏
作用:避免幻数,提高代码可读性