一.结构指针
#include<stdio.h>
struct point {
int x;
int y;
};
void getStruct(struct point);
void output(struct point);
int main(int argc,char const *argv[])
{
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\n",p.x,p.y);
}
void output(struct point p)
{
printf("%d,%d\n",p.x,p.y);
}
- 结构作为参数,是在函数内新建一个结构变量,并复制调用者的结构的值,是外面结构的克隆体,这一点和数组不同。而C语言在函数调用时是传值的,所以以结构作为函数参数读入的值不能送回去。
- 可以在这个输入函数中,创建一个临时的结构变量,然后把这个结构返回给调用者。
#include<stdio.h>
struct point {
int x;
int y;
};
struct point getStruct(void); //函数修改为结构
void output(struct point);
int main(int argc,char const *argv[])
{
struct point y={0,0};
y=getStruct();
output(y);
}
struct point getStruct(void)
{
struct point p;
scanf("%d",&p.x);
scanf("%d",&p.y);
printf("%d %d\n",p.x,p.y);
return p;
}
void output(struct point p)
{
printf("%d,%d\n",p.x,p.y);
}
1.结构指针
struct date{
int month;
int day;
int year;
}myday;
struct date *p = &myday;
(*p).month = 12;
p->month = 12;
- 用->表示指针所指的结构变量中的成员
#include<stdio.h>
struct point {
int x;
int y;
}num;
struct point* getStruct(struct point*);
void output(struct point);
void print(const struct point *p);
int main(int argc,char const *argv[])
{
struct point y={0,0};
getStruct(&y);
output(y);
output(*getStruct(&y));
print(getStruct(&y));
}
struct point* getStruct(struct point *p) //指针
{
scanf("%d",&p->x);//赋值
scanf("%d",&p->y);//赋值
printf("%d %d\n",p->x,p->y);
return p;//返回指针P:传入指针进行处理,然后返回指针,这样可以把它串在其他函数调用中 例如17 18
}
void output(struct point p)
{
printf("%d,%d\n",p.x,p.y);
}
void print(const struct point *p)
{
printf("%d,%d\n",p->x,p->y);
}
2.结构数组
定义:struct date dates[100];
赋值:struct date dates[]={ {4'5,2005},{2,5,2006},...... } //两个大括号。
#include<stdio.h>
struct time{
int hour;
int minutes;
int seconds;
};
struct time timeUpdate(struct time now);
int main(void)
{
struct time testTime[5]={
{11,59,59},{12,0,0},{1,29,59},{23,59,59},{19,12,27}
};
int i;
//遍历
for(i=0;i<5;++i){
printf("Time is %.2i:%.2i:%.2i\n",
testTime[i].hour,testTime[i].minutes,testTime[i].seconds);
testTime[i]=timeUpdate(testTime[i]);
printf("one second later is %.2i:%.2i:%.2i\n",
testTime[i].hour,testTime[i].minutes,testTime[i].seconds);
}
return 0;
}
//定义函数
struct time timeUpdate(struct time now){
++now.seconds;
if(now.seconds == 60){
now.seconds=0;
++now.minutes;
if(now.minutes==60){
now.minutes=0;
++now.hour;
if(now.hour==24){
now.hour=0;
}
}
}
return now;
}
3.结构中的结构
struct dateAndTime{
struct date sdate;
struct time stime;
};
嵌套的结构
struct point {
int x;
int y;
};
表示两个数:
x | y |
struct point {
int x;
int y;
};
struct rectangle{
struct point pt1;
struct point pt2;
};
表示一个矩阵:
r.pt1.x | r.pt1.y |
r.pt2.x | r.pt2.y |
变量定义:
struct rectangle r,*rp;
rp = &r;
则
-
r.pt1.x
-
rp->pt1.x
-
(r.pt1).x
-
(rp->pt1).x
四种形式是等效的
#include<stdio.h>
struct point {
int x;
int y;
};
struct rectangle{
struct point p1;
struct point p2;
};
void printRect(struct rectangle r){
printf("<%d, %d> to <%d, %d>\n",r.p1.x, r.p1.y, r.p2.x, r.p2.y);
}
int main(int argc,char const *argv[])
{
int i;
struct rectangle rects[]={
{{1,2},{3,4}},
{{5,6},{7,8}}
};
for(i=0;i<2;i++){
printRect(rects[i]);
}
}
{1,2} | {3,4} |
{5,6} | {7,8} |
4.自定义数据类型(typedef)
typedef int(原来实际的) Length(新的);
typedef struct ADate {
int month;
int day;
int year;
} Date(新的名字);
Date d={6,5,2019};
- Length 代表int,就可以代替int出现在变量定义和参数声明的地方。
- Date=struct ADate。
Length a,b,len;
Length num[5];
5.联合
union AnElt {
int i;
char c;
}elt1,elt2;
elt1.i = 4;
elt2.c = 'a';
elt2.i = 0xDEADBEEF;