一。復(fù)雜結(jié)構(gòu)類(lèi)型
結(jié)構(gòu)體
聯(lián)合體
枚舉類(lèi)型
1.結(jié)構(gòu)體
基本定義
struct 結(jié)構(gòu)名
{
//成員列表
};//分號(hào)作為結(jié)束
成員列表:
有基本數(shù)據(jù)類(lèi)型定義的變量或者是構(gòu)造類(lèi)型的變量
example:
struct student
{
int grade;
int age;
char name[32];
};
student:結(jié)構(gòu)體名稱(chēng)
struct student:結(jié)構(gòu)數(shù)據(jù)類(lèi)型,相當(dāng)于int,
double,char等基本數(shù)據(jù)結(jié)構(gòu);
struct student stu;
stu:結(jié)構(gòu)體變量
訪(fǎng)問(wèn)結(jié)構(gòu)成員:"."
訪(fǎng)問(wèn)結(jié)構(gòu)體成員:
stu.name;
stu.grade;
stu.age;
2.結(jié)構(gòu)體變量的初始化
#include<stdio.h>
#include<string.h>
struct student
{
char name[32];
char sex;
int age;
};
(1)初始化1
struct student boy;
strcpy(boy.name,"jeck");
boy.age=24;
boy.sex='m';
(2)初始化2
struct student stu1 = {"lily",'f',20}
printf("name:%s\nsex:%c\nage:%d\n",stu1.name,stu1.sex,stu1.age)
注意:初始化時(shí),一定要與成員一一對(duì)齊
(3)初始化3:生命結(jié)構(gòu)體時(shí),定義結(jié)構(gòu)體變量
一 struct student
{
char name[32];
char sex;
int age ;
}stu,stu1;
補(bǔ)上:
嵌套定義結(jié)構(gòu)體:
struct student //(大小不固定)
{
int a;
char b;
//因struct student大小不確定,無(wú)法分配空間
struct student stu;//error
};
struct student
{
int a;
char b;
//指針大小是固定的,可以
struct student *ps;//(*ps大小固定)
};
3.無(wú)名結(jié)構(gòu)體
struct
{
int age;
char name[16];
}stu;
無(wú)名結(jié)構(gòu)體一般不使用
#include<stdio.h>
#include<string.h>
struct student
{
int age;
char name[32];
};
int main()
{
struct stuent stu,stu1;
stu.age = 24;
strcpy(stu.name,"lily";
printf("%s\t%d\n",stu.name,stu.age);
stu1=stu;
printf("%s\t%d\n",stu1.name,stu1.age);
return 0;
}
4.宏定義結(jié)構(gòu)替
struct student
{
char name[32]
char sex;
int age ;
};
#define STU struct student
STU stu,stu1;<------> st
5.結(jié)構(gòu)替的嵌套
struct date
{
int year;
int month;
int day;
};
struct student
{
char name[32];
int age ;
struct date birthday;
};
6.結(jié)構(gòu)體數(shù)組 struct_arr.c
#include<stdio.h>
#include<string.h>
struct student
{
int age
char name[32];
};
int main()
{
//結(jié)構(gòu)替數(shù)組初始化
//struct student stu,stu1,stu2;
struct student arr[3]=
{
{24,"hello"},
{20,"lily"},
{26,"jack"}
};
//結(jié)構(gòu)替訪(fǎng)問(wèn)
printf("arr[1].age=%d\narr[1].name%s\n",arr[1].age,arr[1].name);
return 0;
7.結(jié)構(gòu)體指針
malloc(); //申請(qǐng)堆空間
free(); //釋放空間
//申請(qǐng)一塊堆空間,大小為:sizeof(struct date)
pa= (struct date *)malloc(sizeof(struct date))
free(pa); //釋放申請(qǐng)的堆空間
8.typedef
重新取名
typedef int I
即給int取名為 I;
結(jié)構(gòu)體
typedef struct student
{
int age;
char name[32];
}STU;
STU stu;------>struct student stu;
和宏定義的區(qū)別:
typedef struct student STU
#define STU struct student
9.結(jié)構(gòu)體大小
內(nèi)存對(duì)齊:
Linux: 4字節(jié)
Windows:8字節(jié)
默認(rèn)從偏移量為0的位置開(kāi)始存儲(chǔ)
每個(gè)成員所占字節(jié)是其自身大小的整數(shù)倍.
int [4];short[2];long[8]
10.聯(lián)合體
union untype
{
int a ;
long b;
int arr[4];
};
特點(diǎn):
每次只能操作一個(gè)成員變量?。?!
分配空間;
按最大數(shù)據(jù)類(lèi)型分配空間
11.枚舉類(lèi)型
enum entype
{
A,
b,
c,
}
12.鏈表
鏈?zhǔn)酱鎯?chǔ)結(jié)構(gòu),線(xiàn)性存儲(chǔ)結(jié)構(gòu)
其大小可動(dòng)態(tài)改變,鏈表是由一個(gè)個(gè)結(jié)點(diǎn)串起來(lái)的數(shù)據(jù)鏈
結(jié)點(diǎn):
由數(shù)據(jù)域和指針域組成
數(shù)據(jù)域:存放數(shù)據(jù)
指針域:存放下一個(gè)結(jié)點(diǎn)的地址
(1)創(chuàng)建鏈表
struct student
{
int id;
struct student *next;
};
struct student *head;
malloc()
free()
創(chuàng)建一個(gè)頭結(jié)點(diǎn):
struct student *head;
head = (struct student *)malloc(sizeof(struct student));
頭結(jié)點(diǎn)標(biāo)示一個(gè)鏈表,即鏈表名稱(chēng)
頭結(jié)點(diǎn)的數(shù)據(jù)域不存放數(shù)據(jù),指針域存放第一個(gè)結(jié)點(diǎn)的地址,
頭結(jié)點(diǎn)只是為了標(biāo)示這個(gè)鏈表