結構體數組初始化三種方法,轉載

來源:互聯網
上載者:User

標籤:

C語言結構體初始化的三種方法:原文連結http://www.2cto.com/kf/201503/386575.html

 

12345678910111213141516171819202122232425262728293031323334353637383940 #include <stdio.h> struct student_st{    char c;    int score;    const char *name;}; static void show_student(struct student_st *stu){    printf("c = %c, score = %d, name = %s\n", stu->c, stu->score, stu->name);} int main(void){    // method 1: 按照成員聲明的順序初始化    struct student_st s1 = {‘A‘, 91, "Alan"};    show_student(&s1);     // method 2: 指定初始化,成員順序可以不定,Linux 核心多採用此方式    struct student_st s2 =     {        .name = "YunYun",        .c = ‘B‘,        .score = 92,    };    show_student(&s2);     // method 3: 指定初始化,成員順序可以不定    struct student_st s3 =     {        c: ‘C‘,        score: 93,        name: "Wood",    };    show_student(&s3);         return 0;}</stdio.h>

 

運行結果:

 

如果想初始化結構體數組,可採用 {{ }, { }, { }} 方式,如

12345678910111213 struct student_st stus[2] = {    {        .c = ‘D‘,        .score = 94,        /*也可以只初始化部分成員*/    },    {        .c = ‘D‘,        .score = 94,        .name = "Xxx"    },};

結構體數組初始化三種方法,轉載

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.