zoukankan      html  css  js  c++  java
  • c语言结构体

    在我们的驱动开发中经常用到结构体,下面将结构体的基本用法总结如下:

    #include<stdio.h>
    #include<stdlib.h>

    #define Debug

    struct student{
        int old;   
        int score;
    };

    typedef struct master{
        int old;
        int score;
    }ma;

    typedef struct doctor{
        int old;
        int score;
    }dor,*doctor;

    typedef struct professor{
        int old;
        int score;
        struct master *ma_p;
        doctor doc_p;
    }prof,*professor_p;

    int main()
    {
        struct student stu;
        stu.old=28;
        stu.score=98;
        printf("well struct xmphoenix\n");
    #ifdef Debug
        printf("the student1 is old=%d\n",stu.old);
        printf("the student1 is score=%d\n",stu.score);
    #endif
        ma master;
        ma *p;
        master.old=29;
        master.score=100;
        p=&master;
        printf("the student2 is old=%d\n",p->old);
        printf("the student2 is score=%d\n",p->score);
        p=NULL;

        doctor pt;
        pt=(doctor)malloc(sizeof(dor));
        pt->old=30;
        pt->score=100;
        printf("the student3 is old=%d\n",pt->old);
        printf("the student3 is score=%d\n",pt->score);
        free(pt);
        professor_p ppt;
        ppt=(professor_p)malloc(sizeof(prof));
        ppt->old=32;
        ppt->score=100;
        ppt->ma_p=&master;
        ppt->ma_p->old=100;
        ppt->ma_p->score=200;
        ppt->doc_p=(doctor)malloc(sizeof(dor));
        ppt->doc_p->old=200;
        ppt->doc_p->score=300;
        printf("prof is old =%d\n",ppt->old);
        printf("prof is score =%d\n",ppt->score);
        printf("master in prof is old =%d\n",ppt->ma_p->old);
        printf("master in prof is score =%d\n",ppt->ma_p->score);
        printf("doctor in prof is old =%d\n",ppt->doc_p->old);
        printf("doctor in prof is score =%d\n",ppt->doc_p->score);
        free(ppt->doc_p);
        free(ppt);
        return 0;       
    }

    编译运行结果:

    struct

  • 相关阅读:
    jQuery中时间戳和日期的相互转换
    jquery append 方法应用
    MySQL中实现连续日期内数据统计,缺省天数0补全
    jQuery通过ajax请求php遍历json数组到table中的代码
    sql相同表不同查询条件合并显示
    paginate()出来的数据怎样循环插入数据?
    使用paginate分页后数据处理
    ThinkPhp3.2.3 使用phpExcel导入数据
    判断时间戳是星期几
    英文加数字升序/降序
  • 原文地址:https://www.cnblogs.com/xmphoenix/p/2405686.html
Copyright © 2011-2022 走看看