struct ๊ตฌ์กฐ์ฒด๋ช
{
์๋ฃํ1 ๋ณ์๋ช
1;
์๋ฃํ2 ๋ณ์๋ช
2;
์๋ฃํ3 ๋ณ์๋ช
3;
...
};
๊ตฌ์กฐ์ฒด๋ ๋ณดํต ์ ์ญํจ์์ ๋ง์ฐฌ๊ฐ์ง๋ก main() ํจ์ ์ ์ ์ญ ์์น์ ์ ์ํ๋ค.
์ฌ์ฉ์ ์ ์ ์๋ฃํ
struct GameInfo {
char* name;
int year;
int price;
char* company;
};
๊ตฌ์กฐ์ฒด ์ ์ธ : struct ๊ตฌ์กฐ์ฒด๋ช ๋ณ์๋ช ;
struct GameInfo gameInfo1;
typedef๋ก ๊ตฌ์กฐ์ฒด ์ ์ธํ๊ธฐ
typedef : ์๋ฃํ์ ์๋ก ์ด๋ฆ์ ๋ถ์ผ ๋ ์ฌ์ฉ
int i = 1;
typedef int ์ ์; // int == ์ ์
์ ์ ์ ์๋ณ์ = 3; // int i = 3;
typedef float ์ค์;
์ค์ ์ค์๋ณ์ = 3.23f; // float f = 3.23f;
printf("์ ์๋ณ์ %d, ์ค์๋ณ์ %.2f\n", ์ ์๋ณ์, ์ค์๋ณ์);
์์
int main(void) {
typedef struct GameInfo ๊ฒ์์ ๋ณด;
๊ฒ์์ ๋ณด game1;
game1.name = "ํ๊ธ ๊ฒ์";
game1.year = 2022;
return 0;
}
๋ค๋ฅธ ์นํ ๋ฐฉ๋ฒ (struct GameInformation -> GAME_INFO ๋ก ์นํ)
์ด ๋, GameInformation ์์จ๋ ๋จ. ๊ตฌ์กฐ์ฒด๋ช ์๋ต ๊ฐ๋ฅ.
struct GameInfo {
char* name;
int year;
int price;
char* company;
struct GameInfo* friendGame;
};
typedef struct GameInformation {
char* name;
int year;
int price;
char* company;
struct GameInfo* friendGame;
} GAME_INFO;
int main(void) {
typedef struct GameInfo ๊ฒ์์ ๋ณด;
๊ฒ์์ ๋ณด game1;
game1.name = "ํ๊ธ ๊ฒ์";
game1.year = 2022;
GAME_INFO game2;
game2.name = "ํ๊ธ ๊ฒ์2";
game2.year = 2023;
struct GameInformation game3;
game3.name = "ํ๊ธ ๊ฒ์3";
printf("๊ฒ์ ์ด๋ฆ : %s\n", game1.name);
printf("๋ฐ๋งค ์ฐ๋ : %d\n", game1.year);
printf("๊ฒ์ ์ด๋ฆ : %s\n", game2.name);
printf("๋ฐ๋งค ์ฐ๋ : %d\n", game2.year);
return 0;
}
๋ณ์์ ๊ฐ์ ์ ์ฅ : ๋ณ์๋ช .๋ฉค๋ฒ๋ช = ๊ฐ;
'.' : ๋ฉค๋ฒ ์ฐธ์กฐ ์ฐ์ฐ์
๋ณ์์ ๊ฐ์ ์ ์ฅํ๋ ๋ฐฉ๋ฒ 2๊ฐ์ง
1. ๋ณ์๋ช .๋ฉค๋ฒ๋ช = ๊ฐ;
2. ์ ์ธ๋ ๊ตฌ์กฐ์ฒด ๋ณ์ = {๊ฐ};
#include <stdio.h>
struct GameInfo {
char* name;
int year;
int price;
char* company;
};
int main(void) {
struct GameInfo gameInfo1;
gameInfo1.name = "๋๋๊ฒ์";
gameInfo1.year = 2022;
gameInfo1.price = 50;
gameInfo1.company = "๋๋ํ์ฌ";
printf("๊ฒ์ ์ด๋ฆ : %s\n", gameInfo1.name);
printf("๋ฐ๋งค ์ฐ๋ : %d\n", gameInfo1.year);
printf("๊ฒ์ ๊ฐ๊ฒฉ : %d\n", gameInfo1.price);
printf("์ ์ ํ์ฌ : %s\n", gameInfo1.company);
struct GameInfo gameInfo2 = {"๋๋๊ฒ์", 2022, 100, "๋๋ํ์ฌ"};
printf("๊ฒ์ ์ด๋ฆ : %s\n", gameInfo2.name);
printf("๋ฐ๋งค ์ฐ๋ : %d\n", gameInfo2.year);
printf("๊ฒ์ ๊ฐ๊ฒฉ : %d\n", gameInfo2.price);
printf("์ ์ ํ์ฌ : %s\n", gameInfo2.company);
return 0;
}
๊ตฌ์กฐ์ฒด ๋ฐฐ์ด ๋ง๋ค๊ธฐ
struct ๊ตฌ์กฐ์ฒด๋ช ๋ฐฐ์ด๋ช [๋ฐฐ์ด ํฌ๊ธฐ];
struct GameInfo gameArray[2] = {
{"๋๋๊ฒ์", 2022, 50, "๋๋ํ์ฌ"},
{"๋๋๊ฒ์", 202, 100, "๋๋ํ์ฌ"}
};
๋๋๊ฒ์ | 2022 | 50 | ๋๋ํ์ฌ | ๋๋๊ฒ์ | 2022 | 100 | ๋๋ํ์ฌ |
gameArray[0] | gameArray[1] |
๊ตฌ์กฐ์ฒด ํฌ์ธํฐ ์ฌ์ฉํ๊ธฐ
ํฌ์ธํฐ ๋ณ์๋ ์ด๋ค ๋ฉ๋ชจ๋ฆฌ ๊ณต๊ฐ์ ์ฃผ์๋ฅผ ์ ์ฅํด ํด๋น ์ฃผ์๋ฅผ ๊ฐ๋ฆฌํค๋ ๋ณ์
์ด์ ๋ง์ฐฌ๊ฐ์ง๋ก ๊ตฌ์กฐ์ฒด ํฌ์ธํฐ๋ ๊ตฌ์กฐ์ฒด ๋ณ์์ ์ฃผ์๋ฅผ ๊ฐ๋ฆฌํค๋ ๋ณ์๋ฅผ ์๋ฏธํ๋ค.
struct ๊ตฌ์กฐ์ฒด๋ช * ๋ณ์๋ช ;
struct GameInfo* gamePtr; // ๊ตฌ์กฐ์ฒด ํฌ์ธํฐ ๋ณ์ ์ ์ธ
gamePtr = &gameInfo1;
printf("๊ฒ์ ์ด๋ฆ : %s\n", (*gamePtr).name);
printf("๋ฐ๋งค ์ฐ๋ : %d\n", (*gamePtr).year);
printf("๊ฒ์ ๊ฐ๊ฒฉ : %d\n", (*gamePtr).price);
printf("์ ์ ํ์ฌ : %s\n", (*gamePtr).company);
(*gamePtr).name == gamePtr -> name
-> : ๊ฐ์ ๋ฉค๋ฒ ์ฐธ์กฐ ์ฐ์ฐ์
๊ตฌ์กฐ์ฒด ์์ ๊ตฌ์กฐ์ฒด ์ฌ์ฉํ๊ธฐ
struct GameInfo {
char* name;
int year;
int price;
char* company;
struct GameInfo* friendGame; // ๊ตฌ์กฐ์ฒด ํฌ์ธํฐ
};
friendGame์ GameInfo ๊ตฌ์กฐ์ฒด์ 5๋ฒ์งธ ๋ฉค๋ฒ์ด๋ฉด์ ํฌ์ธํฐ ๋ณ์
๋ฐ๋ผ์, '๋๋ํ์ฌ' ์ ๋ณด๋ฅผ ๊ฐ์ง gameInfo1 ๋ณ์๋ก friendGame ๋ฉค๋ฒ์ ์ ๊ทผํ ์ ์๋ค. ๊ทธ๋ฆฌ๊ณ '๋๋ํ์ฌ' ์ ๋ณด๋ฅผ ๊ฐ์ง gameInfo2 ๋ณ์์ ์ฃผ์๋ฅผ ์ ์ฅํ๊ฒ ๋๋ฉด gameInfo1.friendGame์ด gameInfo2๋ฅผ ๊ฐ๋ฆฌํค๊ฒ ๋์ด ๋ค๋ฅธ ํ์ฌ์ ๊ฒ์ ์ ๋ณด๋ ์ ๊ทผ ๊ฐ๋ฅํ๋ค.
gameInfo1.friendGame = &gameInfo2; // ๋ค๋ฅธ ํ์ฌ์ ๊ฒ์ ์ ๋ณด๋ฅผ ๋ด์ ๋ณ์ ์ฃผ์ ์ ์ฅ
printf("๊ฒ์ ์ด๋ฆ : %s\n", gameInfo1.friendGame->name);
printf("๋ฐ๋งค ์ฐ๋ : %d\n", gameInfo1.friendGame->year);
printf("๊ฒ์ ๊ฐ๊ฒฉ : %d\n", gameInfo1.friendGame->price);
printf("์ ์ ํ์ฌ : %s\n", gameInfo1.friendGame->company);
๊ณ ์์ด ๋ฐ์ค์ ๋ฃ๊ธฐ ํ๋ก์ ํธ
getchar() : ์ฌ์ฉ์์ ์ ๋ ฅ์ ๋ฐ๋ ํจ์
#include <stdio.h>
#include <time.h>
#include <stdio.h>
typedef struct {
char* name;
char* character;
int age;
int level;
} CAT;
CAT cats[5]; // ํฐ ์์์ ์๋ ๊ณ ์์ด ๋ชฉ๋ก
int collection[5] = { 0,0,0,0,0 };
void initCats();
void printCat(int selected);
int checkCollection();
int main(void) {
srand(time(NULL));
initCats();
while (1) {
printf("๋๊ทผ๋๊ทผ! ์ด๋ ๊ณ ์์ด์ ์ง์ฌ๊ฐ ๋ ๊น์? \n์ํฐ๋ฅผ ๋๋ฌ ํ์ธํ์ธ์!");
getchar();
int selected = rand() % 5;
printCat(selected);
collection[selected] = 1;
int collectAll = checkCollection();
if (collectAll == 1) {
break; // ๊ณ ์์ด 5๋ง๋ฆฌ๋ฅผ ๋ชจ๋ ๋ชจ์ผ๋ฉด ๋ฌดํ ๋ฐ๋ณต๋ฌธ ํ์ถ
}
}
return 0;
}
void initCats() {
cats[0].name = "A";
cats[0].age = 5;
cats[0].character = "์จ์ํจ";
cats[0].level = 1;
cats[1].name = "B";
cats[1].age = 3;
cats[1].character = "๊น์น ํจ";
cats[1].level = 2;
cats[2].name = "C";
cats[2].age = 7;
cats[2].character = "์ ์ด ๋ง์";
cats[2].level = 3;
cats[3].name = "D";
cats[3].age = 2;
cats[3].character = "์๋๋ฌ์";
cats[3].level = 4;
cats[4].name = "E";
cats[4].age = 1;
cats[4].character = "๋ ๋ฐฐ๊ณ ํ";
cats[4].level = 5;
}
void printCat(int selected) {
printf("๊ณ ์์ด์ ์ ๋ณด๋ฅผ ์ถ๋ ฅํฉ๋๋ค\n");
printf("์ด๋ฆ : %s\n", cats[selected].name);
printf("๋์ด : %d\n", cats[selected].age);
printf("ํน์ง : %s\n", cats[selected].character);
printf("๋ ๋ฒจ : ");
for (int i = 0; i < cats[selected].level; ++i) {
printf("%s", "โ
");
}
printf("\n\n");
}
int checkCollection() {
int collectAll = 1;
printf("๋ณด์ ํ ๊ณ ์์ด ๋ชฉ๋ก์
๋๋ค. \n\n");
for (int i = 0; i < 5; ++i) {
if (collection[i] == 0) {
printf("%10s", "(๋น ์์)");
collectAll = 0;
}
else {
printf("%10s", cats[i].name);
}
}
printf("\n\n");
if (collectAll) {
printf("\n์ถํํฉ๋๋ค. 5๋ง๋ฆฌ์ ๊ณ ์์ด๋ฅผ ๋ชจ๋ ๋ชจ์์ด์. ์ด์ฌํ ํค์์ฃผ์ธ์\n");
}
return collectAll;
}
๋์ ๋์ฌ ํ๋ก์ ํธ
#include <stdio.h>
typedef struct book {
int id; // ๋ฒํธ(id)
char* title; // ์ ๋ชฉ
int available; // 0 : ๋์ถ ์ค, 1 : ๋์ถ ๊ฐ๋ฅ
} BOOK;
BOOK book[4];
void initBook();
void printBook();
int main(void) {
int bookNum;
initBook();
while (1) {
printBook();
printf("๋์ฌํ ์ฑ
๋ฒํธ๋ฅผ ์
๋ ฅํด์ฃผ์ธ์ (์ข
๋ฃ : -1) : ");
scanf_s("%d", &bookNum);
if (bookNum == -1) {
printf("ํ๋ก๊ทธ๋จ์ ์ข
๋ฃํฉ๋๋ค\n");
break;
}
if (bookNum < -1 || bookNum > 4) {
printf("์๋ชป๋ ์
๋ ฅ์
๋๋ค.\n");
continue;
}
bookNum--;
if (book[bookNum].available == 0) {
printf("%s ๋ฐ๋ฉ์ด ์๋ฃ๋์์ต๋๋ค.\n", book[bookNum].title);
book[bookNum].available = 1;
}
else {
printf("%s ๋์ถ์ด ์๋ฃ๋์์ต๋๋ค.\n", book[bookNum].title);
book[bookNum].available = 0;
}
}
return 0;
}
void initBook() {
book[0].id = 1;
book[0].title = "C ์
๋ฌธํธ";
book[0].available = 0;
book[1].id = 2;
book[1].title = "ํ์ด์ฌ ๊ธฐ๋ณธํธ";
book[1].available = 1;
book[2].id = 3;
book[2].title = "์๋ฐ ๊ธฐ๋ณธํธ";
book[2].available = 0;
book[3].id = 4;
book[3].title = "ํ์ด์ฌ ์ค๊ธํธ";
book[3].available = 1;
}
void printBook() {
for (int i = 0; i < 4; ++i) {
printf("%d.", book[i].id);
printf("%-16s", book[i].title);
if (book[i].available == 1) {
printf("[๋์ถ ๊ฐ๋ฅ]\n");
}
else {
printf("[๋์ถ ์ค]\n");
}
}
}
'๐ฉโ๐ป ํ๋ก๊ทธ๋๋ฐ > ๐ญ C' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[C] ํ์ผ ์ ์ถ๋ ฅ | #define (1) | 2024.01.02 |
---|---|
[C] ๋ค์ฐจ์ ๋ฐฐ์ด (0) | 2023.12.31 |
[C] ํฌ์ธํฐ | ๊ฐ์ ์ํ ํธ์ถ | ์ฐธ์กฐ์ ์ํ ํธ์ถ | clock() | ํ์ ํํฌ (0) | 2023.10.12 |
[C] ๋ฐฐ์ด | null | sizeof() | ์์คํค์ฝ๋ (0) | 2023.09.10 |
[C] ํจ์ (0) | 2023.09.10 |