结构中的结构

结构数组

struct date dates[100];
struct date dates[]={
    {4,5,2005},{2,4,2005}
};
#include<stdio.h>

struct time{
    int hour;
    int minutes;
    int seconds;
};

struct time timeUpdate(struct time now);

int main(void){
    struct time testTimes[5]={
        {11,55,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",
            testTimes[i].hour,testTimes[i].minutes,testTimes[i].seconds);
        
        testTimes[i] = timeUpdate(testTimes[i]);
        
        printf("...one second later it is %.2i:%.2i:%.2i",
            testTimes[i].hour,testTimes[i].minutes,testTimes[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;
            }
        }
    }
}

结构中的结构
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;
//就可以有:
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 

//但是没有rp->pt1->x
//因为pt1不是指针

结构中的结构的数组

#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 i;
	struct rectangle rects[]={
		{{1,2},{3,4}},
		{{5,6},{7,8}}		
	};// 2 rectangles
	for(i = 0;i<2;i++){
		printRect(rects[i]);
	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值