-
Notifications
You must be signed in to change notification settings - Fork 0
/
01_Struct.c
53 lines (41 loc) · 1.22 KB
/
01_Struct.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <stdio.h>
struct Character {
int hp, mp, lv;
float exp;
char initial1, initial2, initial3;
}; // 메모리는 4byte씩 할당합니다.
struct Character CharacterConstructor(int wantHp, int wantMp, int wantLv) {
struct Character result;
result.hp = wantHp;
result.mp = wantMp;
result.lv = wantLv;
result.exp = 0;
return result;
};
void printCharacter(struct Character target) {
printf("체력 : %d\n", target.hp);
printf("마나 : %d\n", target.mp);
printf("레벨 : %d\n", target.lv);
printf("경험치 : %.02f\n", target.exp);
}
void takeDamage(struct Character target, int damage) {
target.hp -= damage;
printCharacter(target); // 함수의 매개변수를 전달하는 것
}
int main() {
int myHp, myMp, myLv, myExp;
int slimeHp, slimeMp, slimeLv, sliemExp;
struct Character player;
player.hp = 100;
player.mp = 100;
player.lv = 2;
player.exp = 3.14f;
printf("플레이어의 크기 : %d byte\n\n", sizeof(player));
printf("플레이어의 체력 : %d\n", player.hp);
printf("플레이어의 마나 : %d\n", player.mp);
printf("플레이어의 레벨 : %d\n", player.lv);
printf("플레이어의 경험치 : %.02f\n\n", player.exp);
struct Character monster = CharacterConstructor(30, 10, 1);
takeDamage(monster, 20);
printCharacter(monster);
}