2020年5月3日 星期日

C 語言 - struct array 初始化 ( Initializing array of structures )

初始化 struct array

    struct student
    {
        char* name;
        int grade;
        int id;
    };

1. 依照當初宣告的順序

    struct student myStudents[] = {
        {"Henry", 3, 1},
        {"Marry", 3, 2}
    };

1. 依照當初宣告的名子

    struct student myStudents[] = {
        {.id = 1, .grade = 3, .name = "Henry"},
        {.id = 2, .grade = 3, .name = "Marry"}
    };

Output

  printf("ID : %d-%d\tName : %s\n", myStudents[0].grade, myStudents[0].id, myStudents[0].name);
  printf("ID : %d-%d\tName : %s\n", myStudents[1].grade, myStudents[1].id, myStudents[1].name);

  // ID : 3-1        Name : Henry
  // ID : 3-2        Name : Marry


參考資料 :
https://stackoverflow.com/questions/18921559/initializing-array-of-structures

0 意見:

張貼留言

Popular Posts