十三、结构类型(4)——结构中的结构

结构数组

 struct date dates[100];
   struct date dates={
       {4,5,2005 },{2,4,2005}
   };

结构中的结构

struct dateAndTime{
    strcut 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             <=>   (r.pt1).x
<=>   rp->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>
",r.p1.x,r.p1.y,r.p2.x,r.p2.y);
 } 

void main()
{
     
    int i;
    struct rectangle rects[]={      //结构数组中有2个rectangle
        {
            {1,2},{3,4}
        },
        {
            {5,6},{7,8}
        }
    }; 
    
    for(i=0;i<2;i++)
    {
        printRect(rects[i]);
    }
    
}

原文地址:https://www.cnblogs.com/Strugglinggirl/p/9080070.html