3.8 KiB
3.8 KiB
目录
构造类型
结构体
产生的原因及意义
存放不同类型的数据到一起。
类型的描述
struct 结构体名
{
数据类型 成员1;
数据类型 成员2;
......
};
嵌套定义
定义变量(变量,数组,指针),初始化及成员引用
成员引用: 变量名. 成员名,指针->成员名,(*指针).成员名
结构体占用的内存空间大小
函数传参(值,地址)
#include <stdio.h>
#include <stdlib.h>
#define NAMESIZE 32
struct
{
int i;
char ch;
float f;
} a =
{
1,
'a',
},
b, c, *p, *q;
struct simp_st
{
int i;
char ch;
float f;
// char ch1;
}; // __attribute__((packed));
struct student_st
{
int id;
char name[NAMESIZE];
// struct birthday_st birth;
struct birthday_st
{
int year;
int month;
int day;
} birth;
int math;
int chinese;
};
void func(struct simp_st *b)
{
printf("%d\n", sizeof(b));
}
int main()
{
// TYPE NAME = VALUE;
struct simp_st a;
struct simp_st *p = &a;
// func(a);
func(p);
// printf("sizeof(point) = %d\n", sizeof(p));
// printf("sizeof(struct) = %d\n", sizeof(a));
// out:
// sizeof(point) = 8
// sizeof(struct) = 12
#if 0
struct student_st stu = {10011, "Alan", {2011, 11, 11}, 98, 97};
struct student_st *p = &stu;
struct student_st arr[2] = {{.name = "Alan"}, {.name = "John"}};
p = &arr[0];
// struct student_st stu = {.math = 98, .chinese = 97};
// printf("%d %s %d-%d-%d %d %d\n",
// stu.id,
// stu.name,
// stu.birth.year,
// stu.birth.month,
// stu.birth.day,
// stu.math,
// stu.chinese);
// printf("%d %s %d-%d-%d %d %d\n",
// p->id,
// p->name,
// p->birth.year,
// p->birth.month,
// p->birth.day,
// p->math,
// p->chinese);
for (int i = 0; i < 2; i++, p++)
{
printf("%s ", p->name);
}
printf("\n");
#endif
#if 0
struct simp_st a = {123, 456.789, 'a'};
a.i = 112233;
printf("%d %f %c", a.i, a.f, a.ch);
#endif
exit(0);
}
跳过
共用体
产生及意义
类型描述
union 共用体名
{
数据类型 成员名1;
数据类型 成员名2;
......
};
嵌套定义
定义变量(变量,数组,指针),初始化及成员引用
占用内存大小
函数传参(值,地址)
位域
枚举
enum 标识符
{
成员1;
成员2;
成员3;
};