结构体
结构体类型设计
1)结构体定义的花括号后边必须加分号。
2)结构体是一种数据类型,是创建变量的末班,不占用内存空间;结构体变量才包含了实实在在的数据,需要存储空间。
3)结构体变量初始化
4)访问结构体
一般格式为:结构体变量.成员名
5)结构体变量和指针
内置类型·能够定义指针变量,结构体类型也可以定义结构体类型指针。
结构体·类型·指针访问成员的获取和赋值形式:
(
p).成员名(括号不能少,·优先级高于
)
p->成员名(->中间不能有空格)
示例:
struct Inventory
{
char description[20];
int quality;
};
int main()
{
struct Inventory sta = { "iphone",20 };
struct Inventory* stp = &sta;
char name[20] = { 0 };
int num = 0;
(*stp).quality = 30;
stp->quality = 30;
strcpy(name, stp->description);
printf("%s %d\n", stp->description, stp->quality);
printf("%s %d\n", (*stp).description, (*stp).quality);
return 0;
}
结构体变量和函数
struct School
{
char s_name[20];
int s_age;
};
void Print_c(struct School* sp)
{
printf("%s %d\n", sp->s_name, sp->s_age);
}
int main()
{
struct School sx = { "tulun",100 };
Print_c(&sx);
}
结构体与数组
所谓结构体数组,是指数组中的每个元素都是一个结构体类型。
示例:
struct School
{
char s_name[20];
int s_age;
float score;
};
int main()
{
struct Student cla[] =
{
{"张三",18,143,5},
{"李四",19,130.6},
{"王五",20,137,9},
{"李华",21,146.9},
{"钱尔",22,150.0},
};
return 0;
}
结构体大小
存储变量地址对齐的3条规则:
1.结构体变量的首 地址,必须是结构体变量中的”最大基本数据类型成员所占字节数”的整数倍。
2.结构体变量 中的每个成员相对于结构体首地址的偏移量,都是该成员基本数据类型所占字节数的整数倍。
3.结构体变量的总大小, 为结构体变量中“最大基本数据类型成员所占字节数’的整数倍。
示例:
此时占12字节
大小端存放问题
1.大端存放:高位字节放在内存的低地址端,低位字节排放在内存的高地址端。
小端模式:低位字节排放在内存的低地址端,高位字节排放在内存的低地址端。
2.如何测试编译器是大端存放还是小端存放
union test
{
char a;
int b;
}data;
int Is_lsb()
{
data.b = 0x12345678;
if (data.a == 0x78)
{
return 1;
}
else
return 0;
}
int main()
{
if (Is_lsb())
{
printf("小端");
}
else
printf("大端");
return 0;
}
int main()
{
int a = 0x12345678;
return 0;
}
结构体镶嵌结构体 结构体镶嵌联合体( 面试必考)
结构体类型
#include<iostream>
using namespace std;
typedef struct AA
{
//4
int id;
// 4不是8的整数倍 (4+4)+8
double weight;
//16是1的整数倍 16
float height;
}A;
所求结构体大小(17)是否是结构体中最宽成员(8)的整数倍;字节填充成为24.
int main()
{
//24
cout<<sizeof(A)<<endl;
}
联合体类型
#include<iostream>
using namespace std;
typedef union BB
{
// 4
int id;
// 20
char ar[20];
// 8
double d;
}B;
**//20不是8的整数倍,字节填充24** 空间共用,只看最大空间与最大字节类型对齐
void main()
{
cout<<sizeof(B)<<endl;
}
结构体镶嵌联合体
#include<iostream>
using namespace std;
typedef union BB
{
int id;
char ar[20];
double d;
}B;
typedef struct AA
{
int id;
double weight;
float height;
//(20 +4)+24
B b;
}A;
void main()
{
cout<<sizeof(A)<<endl;//48
cout<<sizeof(B)<<endl;//24
}
结构体镶嵌结构体、联合体
#include<iostream>
using namespace std;
typedef union BB
{
int id;
char ar[20];
double d;
}B;
typedef struct AA
{
int id;
double weight;
float height;
//24
B b;
}A;
typedef struct CC
{
//4
int id;
//4是1的整倍数,4 +3
char weight[3];
// 7不是8的整倍数,(7+1)+8
double height;
//48
A a;
}C;
void main()
{
//48
cout<<sizeof(A)<<endl;
//24
cout<<sizeof(B)<<endl;
//64
cout<<sizeof(C)<<endl;
}
参考:
https://blog.csdn.net/sinat_42317387/article/details/83755440
struct内镶嵌结构体两种形式
1、当结构体内部镶嵌的结构体有名字时,把它当成类型处理不占空间,不计算在外面结构体大小里面。
2、当结构体内部镶嵌的结构体没有有名字时,计算在结构体大小里面;且按8字节对齐。
#include<iostream>
using namespace std;
struct Test1
{
int a;//4
struct
{
char b;//1
short c;//1+1 +2
double d;//4+4 +8
};
int e;//4
struct Test2
{
int a;//4
struct aa//有名字
{
char b;
short c;
double d;
};
int e;//4
};
void main()
{
cout<<sizeof(struct Test1 )<<endl;//32
cout<<sizeof(struct Test2 )<<endl;//16
}