美文网首页
结构:结构中的结构

结构:结构中的结构

作者: 爱生活_更爱挺自己 | 来源:发表于2020-11-08 17:49 被阅读0次

结构数组

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(int argc, char const *argv[])
{
    struct time testTimes[5] = {
        {11,59,59},{12,0,0},{1,29,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's %.2i:%.2i:%.2i\n", 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;
            }
        }
    }

    return now;
}

Time is 11:59:59...one second later it's 12:00:00
Time is 12:00:00...one second later it's 12:00:01
Time is 01:29:59...one second later it's 01:30:00
Time is 19:12:27...one second later it's 19:12:28
Time is 00:00:00...one second later it's 00:00:01

结构中的结构

struct dataAndTime{
    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 rectagle r,*rp;
    rp = &r;
那么下面的四种形式是等价的:
    r.pt1.x
    rp->pt1.x
    (r.pt1).x
    (rp->pt1).x
但是没有rp->pt1->(因为pt1不是指针)

相关文章

  • 结构:结构中的结构

    结构数组 结构中的结构 嵌套的结构

  • 结构体

    结构体定义* 结构体中的格式:* struch 结构体名* {* 结构体成员变量* }* 结构体中的特点* 1.结...

  • 2.逻辑结构(一):顺序结构

    今天我们开始学习计算机科学中的逻辑结构。逻辑结构有三种:顺序结构、循环结构、条件结构(分支结构)。 顺序结构:计算...

  • 数据结构之一

    按照视点的不同,我们把数据结构分为逻辑结构和物理结构。 逻辑结构 1.集合结构 集合结构:集合结构中的数据元素除了...

  • 数据结构-线性表

    基础概念 数据结构的分类 在数据结构中,按照不同的角度,数据结构分为逻辑结构和物理结构(存储结构)。 逻辑结构:指...

  • 数据处理VBA篇:程序控制结构

    结构化程序设计中基本的3中控制结构:顺序结构,选择结构,循环结构。其中,最基本的是顺序结构,它是后两种的基础。注:...

  • 数据结构(一):结构概念 与 算法概念 及 时间复杂度

    数据结构概念 数据结构分为:逻辑结构、物理结构 逻辑结构 集合结构:集合结构中的数据元素除了同属于一个集合外,它们...

  • 数据结构的三要素

    数据结构主要关注逻辑结构、数据的运算和物理结构(存储结构)。 01 逻辑结构 集合结构和数学中的集合概念类似,各个...

  • 逻辑结构和物理存储结构

    数组是数据结构中的物理结构,建议先搞明白数据结构里的逻辑结构和物理存储结构。

  • 详谈树结构(传统树、字典树、hash 树、Merkle Patr

    关于数据结构中树结构的相关分享 本文参考: 树结构参考文献 一、传统的数据结构中的树结构 树结构是一种非线性存储结...

网友评论

      本文标题:结构:结构中的结构

      本文链接:https://www.haomeiwen.com/subject/mkmhbktx.html