昨天在MOOC上看了一些关于结构体的知识,边听边记了一些关于结构体的笔记,现在把笔记贴出来分享一下。
一、枚举
枚举是一种用户定义的数据类型,关键字是enum:
enum 枚举类型名字 {name1,name2,name3…};
括号里为常量符号,类型为int,值则从0到n。
定义枚举的意义是给常量值名字。。
声明枚举时候也可以指定值。
enum {name1=0,name2=4};
//枚举在C是一个不太成功的东西,主要的目的是定义符号量
二、结构类型
1、声明结构类型
通常把结构声明放在函数外面,方便所有函数使用。
struct point
{
int x;
int y;
int year;
}; //声明新的结构类型
struct point p1,p2; //定义这种类型的变量
无名结构:
struct {
int x;
int y;
}p1,p2;
struct point{
int x;
int y;
}p1,p2;
//可以通过p1,p2去访问x,y
p1,p2叫结构变量,x,y叫做结构成员。
2、结构的初始化
struct date{
int month;
int day;
int year;
}
int main()
{
struct date today={7,31,2016};
struct date thismonth={ .month =7, . year = 2016}; //没有赋值的自动填0,字符型成员 为\0,指针型结构成员初始为NULL。
}
3、结构成员(可以是不同的类型)
用.成员名字 来访问结构里面的成员
4、结构运算
struct point{
int x;
int y;
}p1,p2;
p1= (struct point ){5,10}; // 强制类型转换,相当于p1,x=5,p1.y=10;
p1=p2; //相当于 p1.x=p2.x p1.y=p2.y 数组不能做这样的赋值,因为数组是const 类型,只能改变其中的单个元素。
三、结构与函数
1、结构作为函数参数
struct date{
int month;
int year;
int day;
}
int numberOfDays(struct date d)
整个结构的值可以作为参数传入函数。
struct date{
int month;
int day;
int year;
}today;
today.month=1;
today.year = 2013;
today.day =4;
int numberOfDays(struct date d);
在函数里可以这样使用:
d.month,d.year,d.day;
结构变量可以直接在函数中传来传去。
2、输入结构
include struct point { int x; int y; };
void getStruct(struct point);
void output(struct point);
void main( )
{
struct point y = {0, 0};
getStruct(y); output(y);
}
void getStruct(struct point p)
{
scanf("%d", &p.x);
scanf("%d", &p.y);
printf("%d, %d", p.x, p.y);
}
void output(struct point p)
{
printf("%d, %d", p.x, p.y);
}
并没有改变结构变量y.
函数接收结构变量时是把结构变量中结构成员的的值进行传递
解决方案:
(1) not wise
#include struct point { int x; int y; };
struct point getStruct(void);
void output(struct point);
void main( )
{
struct point y = {0, 0};
y=getStruct(y); //结构变量允许这样赋值
output(y);
}
struct point getStruct(void);
{
struct point p;
scanf("%d", &p.x);
scanf("%d", &p.y);
printf("%d, %d", p.x, p.y);
return p;
}
void output(struct point p)
{
printf("%d, %d", p.x, p.y);
}
(2)结构指针作为函数参数
a.指向结构的指针
struct date{
int month;
int year;
int day;
}myday;
struct date *p=&myday; //取myday的地址
(*p).month =12; //通过指针来访问myday的结构成员
p->month =12; //或者用这种方式来访问
#include struct point { int x; int y; };
struct point getStruct(void);
void output(struct point);
void main( )
{
struct point y = {0, 0};
getStruct(&y);
output(y);
output(*getStruct(&y));
print(gerStruct(&y));
*getStruct(&y)=(struct point ){1,2};
}
struct point *getStruct(struct point *p)
{
scanf("%d", &p->x);
scanf("%d", &p->y);
printf("%d, %d", p->x, p->y);
return p;
}
void output(struct point p)
{
printf("%d, %d", p.x, p.y);
}
void print(const struct point *p)
{
printf("%d%d",p->x,p->y);
}
三、结构数组
struct date dates[100];
struct date dates={{5,4,2012},{6,8,2013}}; // eg. dates[0].month =5
四、结构中的结构
struct dateAndTime
{
struct date sdate;
struct time stime;
}
表示一个矩形:
struct point
{
int x;
int y;
};
struct rectangle
{
struct point pt1;
struct point pt2;
};
struct rectangle r,*rp;
r.pt1.x 、 r.pt1.y
r.pt2.x 、 r.pt2.y
rp=&r;
r.pt1.x;
rp->pt1.x
(r.pt1).x;
(rp->pt1).x
假如我们要做一个rectangle 的数组
struct rectangle rects[]=
{
{{1,2},{3,4}},
{{5,6},{7,8}}
}; //2 rectangles
五、类型定义
C语言提供了一个叫做typedef的功能来声明一个已有的数据类型的新名字。
typedef long long double lld; // 即typedef到最后一个lld之间的东西都用lld替换。
看下面这个例子
typedef struct
{
int month;
int day;
int year;
}Date;
1.如果没有typedef:我们有一个没有名字的struct 有一个变量叫做Date
2.有typedef这样的一个struct 名字叫做Date
六、联合
union AnElt
{
int i;
char c;
}elt1,elt2;
elt1.i=4;
elt.c='a';
与struct 不同的是,union这里的两个成员变量在内存中占据同一个存储空间。