• [C puzzle book] Structure


    #include <stdio.h>
    
    #define PR(format,value) printf(#value"= %"#format"\t",(value))
    #define NL putchar('\n')
    
    #define PRINT1(f,x1) PR(f,x1), NL
    #define PRINT2(f,x1,x2) PR(f,x1), PRINT1(f,x2)
    #define PRINT3(f,x1,x2,x3) PR(f,x1), PRINT2(f,x2,x3)
    #define PRINT4(f,x1,x2,x3,x4) PR(f,x1), PRINT3(f,x2,x3,x4)
    #include "defs.h"
    
    int main(void)
    {
        static struct s1 {
            char c[4], *s;
        } s1 = {"abc","def"};
    
        static struct s2 {
            char *cp;
            struct s1 ss1;
        } s2 = {"ghi", {"jkl","mno"}};
    
        PRINT2(c,s1.c[0],*s1.s);
        PRINT2(s,s1.c,s1.s);
    
        PRINT2(s,s2.cp,s2.ss1.s);
        PRINT2(s,++s2.cp,++s2.ss1.s);
    }
    
    
    //Simple Structure, nested structure;
    #include "defs.h"
    
    struct s1 {
        char *s;
        int i;
        struct s1 *s1p;
    };
    
    int main(void)
    {
        static struct s1 a[] = {
            { "abcd" , 1, a+1},
            { "efgh", 2, a+2},
            { "ijkl", 3, a}
        };
    
        struct s1 *p = a;
        int i;
    
        PRINT3(s,a[0].s,p->s,a[2].s1p->s);
    
        for(i=0; i<2; i++) {
            PR(d,--a[i].i);
            PR(c,++a[i].s[3]);
            NL;
        }
    
        PRINT3(s,++(p->s),a[(++p)->i].s,a[--(p->s1p->i)].s);
    }
    #include "defs.h"
    
    struct s1 {
        char *s;
        struct s1 *s1p;
    };
    
    int main(void)
    {
        static struct s1 a[] = {
            {"abcd", a+1},
            {"efgh",a+2},
            {"ijkl",a}
        };
    
        struct s1 *p[3];
        int i;
    
        for (i=0; i<3; i++ ) 
            p[i] = a[i].s1p;
           PRINT3(s,p[0]->s,(*p)->s,(**p).s);
    
           swap(*p,a);
           PRINT3(s,p[0]->s,(*p)->s,(*p)->s1p->s);
    
           swap(p[0],p[0]->s1p);
           PRINT3(s,p[0]->s,(*++p[0]).s,++(*++(*p)->s1p).s);
           
    }
    
    swap(p1,p2)
    struct s1 *p1,*p2;
    { char *temp;
        temp = p1->s;
        p1->s = p2->s;
        p2->s =temp;
    }
        
    //Array of Pointer to Structures
  • 相关阅读:
    c++ heap学习
    超长正整数相加
    Search Insert Position
    strcpy与strcat函数原型
    C++基本数据类型占字节数
    详解指针的指针
    Google 超分辨率技术 RAISR
    elementui resetFields方法重置表单失败
    VS 点击文件自动定位到解决方案资源管理器中文件所在目录位置
    mybatis中LIKE模糊查询的几种写法以及注意点
  • 原文地址:https://www.cnblogs.com/abacuspix/p/2631730.html
Copyright © 2020-2023  润新知