c語言結構體

來源:互聯網
上載者:User

在我們的驅動開發中經常用到結構體,下面將結構體的基本用法總結如下:

#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;       
}

編譯運行結果:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.