# 結構
## 什麼是結構 📖
之前介紹的int、char...,這些算是基本型別
而結構於他們不同,是一種複合型別
簡單來說,就是多個型別組合成一個
## 用法
### 宣告
``` c=
struct 名稱
{
名稱裡的某一個資料
名稱裡的某一個資料
};
```
### 示範Code👉
```c=
#include <stdio.h>
struct bank
{
int account;
unsigned money;
};
int main()
{
struct bank WU_Bank = {.account = 48763, .money = 10000000};
printf("Ruser in Wu_Bank\n");
printf("account is %d and has %d $",WU_Bank.account,WU_Bank.money);
return 0;
}
```
### typedef用法
因為每次呼叫一個型別都要打上 struct 名稱 太冗長、麻煩
所以可以利用 typedef 來簡化
### 示範Code👉
```c=
#include <stdio.h>
typedef struct
{
int account;
unsigned money;
}bank;
int main()
{
bank WU_Bank = {.account = 48763, .money = 10000000};
printf("Ruser in Wu_Bank\n");
printf("account is %d and has %d $",WU_Bank.account,WU_Bank.money);
return 0;
}
```
### 結構陣列
運用陣列去儲存多個結構
例子:
假設我們今天有一個人的struct 那今天有三個人
就可以運用陣列去存
### 示範Code👉
```c=
#include <stdio.h>
typedef struct
{
int age;
char name[30];
} people;
int main()
{
people All_Person[3] = {
{.name = "Andy", .age = 18},
{.name = "Amy", .age = 23},
{.name = "Sammy", .age = 13}
};
printf("%5s , %d\n", All_Person[0].name, All_Person[0].age);
printf("%5s , %d\n", All_Person[1].name, All_Person[1].age);
printf("%5s , %d\n", All_Person[2].name, All_Person[2].age);
}
```
## 注意❗
### C 的 struct 是不能被賦值的
假設我們想預設每個人都是 18 歲
```c=
typedef struct
{
int age = 18;
char name[30];
} people;
```
會發現上述的程式碼出錯
### 可以將一個創好的 結構 放入另一個 結構
假設有一個銀行裡面有幾個客戶
銀行裡有客戶的帳號 跟 客戶
客戶裡有客戶的名字 跟 年齡
```c=
typedef struct
{
int age;
char name[30];
} People;
typedef struct
{
People people;
char acount[30];
} WU_Bank;
```
### 如果想賦於字串時
不可這樣 ❗
```c=
#include <stdio.h>
typedef struct
{
int age;
char name[30];
} people;
int main()
{
people People;
People.name = "Amy";
return 0;
}
```
要利用 strcpy
```c=
#include <stdio.h>
#include <string.h>
typedef struct
{
int age;
char name[30];
} people;
int main()
{
people People;
strcpy(People.name,"Amy");
return 0;
}
```
<!-- 指標的部分就跳過-->
## 練習
[W3school](https://www.w3schools.com/c/exercise.php?filename=exercise_structs1)
[FCUOJ練習題](https://hackmd.io/@FCU1B112/HJF1N9oN6)
## codeByClass1
``` c=
#include <stdio.h>
#include <string.h>
#define max 2
typedef struct
{
char name[1000];
int height;
int weight;
int age ;
} People;
typedef struct
{
People people[72];
char name_class[20];
}class;
int main()
{
class FCU112 = {.name_class = "FCU1121",
.people[0].name = "ruser",.people[0].age = 18,.people[0].weight = 100,.people[0].height = 180,
.people[1].name = "Winne",.people[1].age = 23,.people[1].weight = 60,.people[1].height = 170};
printf("%s\n",FCU112.name_class);
for(int i = 0; i < max; i++) {
printf("%5s, %3d , %3d , %3d\n",FCU112.people[i].name,FCU112.people[i].age,FCU112.people[i].weight,FCU112.people[i].height);
}
}
```
## codeByClass2
```c=
#include <stdio.h>
#include <string.h>
#define max 2
typedef struct
{
char name[1000];
int height;
int weight;
int age;
} People;
typedef struct
{
People people[72];
char name_class[20];
int number;
} class;
int main()
{
class FCU112;
printf("Today we have many people in here");
printf("What the class name?");
scanf("%s", FCU112.name_class);
printf("How many student?(72down)\n");
scanf("%d", &FCU112.number);
for (int i = 0; i < FCU112.number; i++)
{
printf("Please input infomation of student %d\n", i + 1);
scanf("%s", FCU112.people[i].name);
scanf("%d %d %d", &FCU112.people[i].height, &FCU112.people[i].weight, &FCU112.people[i].age);
}
printf("\n");
for (int i = 0; i < FCU112.number; i++)
{
printf("%5s,%3d,%3d,%3d\n", FCU112.people[i].name, FCU112.people[i].age, FCU112.people[i].weight, FCU112.people[i].height);
}
}
```
## 參考
[結構](https://opensourcedoc.com/c-programming/struct/)