昨天生病提前回去了,所以部分內容沒有聽到,會在這兩天盡快趕上。
函數定義
不能嵌套定義
函數名不能與系統函數重名
函數的返回值
函數指針
指針函數
==========================================================
復雜數據類型
結構體
聯合體
枚舉類型
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 studentpb;
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));
頭結點標識一個鏈表,即鏈表名稱