12.20学习总结

2016-12-21  本文已影响0人  Tangjiayue92

昨天生病提前回去了,所以部分内容没有听到,会在这两天尽快赶上。
函数定义
不能嵌套定义
函数名不能与系统函数重名
函数的返回值
函数指针
指针函数

==========================================================

复杂数据类型
结构体
联合体
枚举类型

1.结构体
基本定义
struct 结构名
{
//成员列表
};
成员列表:
有基本数据类型定义的变量或者是构造类型的变量
example
struct student
{
int grade;
int age;
char name[32];
};
student:结构体名称
struct student:数据结构类型,相当于int,double,char等基本数据类型;
struct student stu;
stu:结构体变量
访问结构成员:“.”
访问结构体成员:
stu.name;
stu.grade;
stu.age;

结构体变量的初始化
struct student
{
char name[32];
char sex;
int age;
};

 初始化1
 struct student boy;
 strcpy(boy.name,"jack");
 boy.sex='m';
 boy.age=24;

 初始化2
 struct student girl={‘lily’,‘f’,23};

 初始化3

无名结构体
struct
{
int age;
char name[16];
}stu;
无名结构体一般不使用

宏定义结构体
宏是原地替换
struct student
{
char name[32];
char sex;
char age;
};

define STU struct student

结构体的嵌套
struct birthday date
{
int year;
int month;
int day;
};
struct student
{
char name[32];
int age;
struct birthday date;
};

int main()
{
struct student stu;
strcpy(stu.name,"hello");
stu.age=31;
stu.birthday.year=1900;
stu.birthday.month=5;
stu.birthday.day=6;
printf("%s,%d,%d,%d,%d\n",stu.name,stu.age,stu.birthday.year,stu.birthday.month,stu.birthday.day);
}

结构体指针
{
结构体
};
struct datepa;
struct student
pb;

include<stdlib.h>

malloc(); //申请空间
free(); //释放空间
//申请一块堆空间,大小为:sizeof(struct student)
pa=(struct date*)malloc(sizeof(struct date));
free(pa);

typedef
重新取名
typedef int I;
即给int取了别名I;
结构体

结构体的大小
内存对齐:
linux:4字节
windows:8字节
默认从偏移量为0的位置开始储存
int 从第四个开始存
long 从第八个开始存

联合体
union untype
{
int a;
long b;
};
特点
最多只能操作一个成员变量
分配空间按最大数据类型分配

枚举类型
enum entype
{
A,
B,
C=12
D,
E
};

int main()
{
printf("A=%d\n",A);
printf("b=%d\n",B);
:
:
:
输出为 0,1,12,13,14

switch case
case要记得和break配套使用

enum type
{
A,
B,
C
};

int main()
{

}

12.链表

 链式存储结构,线性储存结构

(1)创建链表
struct student
{
int id;
struct student *head;
};

     struct student *head;
     malloc()
     free()
     创建一个头节点:
     struct student *head;
    head=(struct student *)malloc(sizeof(struct student));
    头结点标识一个链表,即链表名称
上一篇 下一篇

猜你喜欢

热点阅读