728x90

๊ตฌ์กฐ์ฒด

  • ๊ตฌ์กฐ์ฒด๋Š” ํ•˜๋‚˜ ์ด์ƒ์˜ ๋ณ€์ˆ˜(ํฌ์ธํ„ฐ ๋ณ€์ˆ˜, ๋ฐฐ์—ด ํฌํ•จ)๋ฅผ ํ•˜๋‚˜๋กœ ๋ฌถ์„ ์ˆ˜ ์žˆ๋Š” ๋ณตํ•ฉ ์ž๋ฃŒํ˜•์œผ๋กœ, ๋‹ค์–‘ํ•œ ํ˜•ํƒœ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ฒ˜๋ฆฌํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.
  • ๊ธฐ๋ณธ ์ž๋ฃŒํ˜• ๋ณ€์ˆ˜๋ฅผ ๋ฌถ์–ด์„œ ์ƒˆ๋กœ์šด ์ž๋ฃŒํ˜•์„ ๋งŒ๋“  ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— '์‚ฌ์šฉ์ž ์ •์˜ ์ž๋ฃŒํ˜•'์ด๋ผ๊ณ  ํ•œ๋‹ค.
  • struct ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ •์˜
struct ๊ตฌ์กฐ์ฒด์ด๋ฆ„
{
    ๋ฉค๋ฒ„๋ณ€์ˆ˜1์ž๋ฃŒํ˜• ๋ฉค๋ฒ„๋ณ€์ˆ˜1์ด๋ฆ„;
    ๋ฉค๋ฒ„๋ณ€์ˆ˜2์ž๋ฃŒํ˜• ๋ฉค๋ฒ„๋ณ€์ˆ˜2์ด๋ฆ„;
    ...
};

 

ex) ํ•™์ƒ์— ๋Œ€ํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ํ•˜๋‚˜๋กœ ๋ชจ์€๋‹ค๋ฉด ํ•™๋ฒˆ, ์ด๋ฆ„, ์ „๊ณต, ํ•™์  ๋“ฑ์˜ ์ •๋ณด๊ฐ€ ์žˆ์„ ๋•Œ

๋‹ค์Œ ๊ตฌ์กฐ์ฒด๋Š” ํ•™์ƒ์˜ ํ•™๋ฒˆ, ์ด๋ฆ„, ์ „๊ณต, ํ•™์ ์˜ ์ •๋ณด๋ฅผ ๋ฌถ์„ ์ˆ˜ ์žˆ๋„๋ก ์ •์˜๋œ ๊ตฌ์กฐ์ฒด์ด๋‹ค.


 

๊ตฌ์กฐ์ฒด ์„ ์–ธ

struct ๊ตฌ์กฐ์ฒด์ด๋ฆ„ ๊ตฌ์กฐ์ฒด๋ณ€์ˆ˜์ด๋ฆ„;

struct student student1;

 

  • ๊ตฌ์กฐ์ฒด์˜ ์ •์˜์™€ ๊ตฌ์กฐ์ฒด ๋ณ€์ˆ˜์˜ ์„ ์–ธ์„ ๋™์‹œ์— ํ•  ์ˆ˜๋„ ์žˆ๋‹ค.
struct ๊ตฌ์กฐ์ฒด์ด๋ฆ„
{
    ๋ฉค๋ฒ„๋ณ€์ˆ˜1์ž๋ฃŒํ˜• ๋ฉค๋ฒ„๋ณ€์ˆ˜1์ด๋ฆ„;
    ๋ฉค๋ฒ„๋ณ€์ˆ˜2์ž๋ฃŒํ˜• ๋ฉค๋ฒ„๋ณ€์ˆ˜2์ด๋ฆ„;
    ...
}๊ตฌ์กฐ์ฒด๋ณ€์ˆ˜์ด๋ฆ„;
struct student
{
    int number;
    char name[20];
    char major[20];
    double grade;
 }student1;

๊ตฌ์กฐ์ฒด ๋ฉค๋ฒ„ ์ ‘๊ทผ

  • ๋ณ€์ˆ˜๋กœ ์„ ์–ธํ•œ ๊ตฌ์กฐ์ฒด์˜ ๋ฉค๋ฒ„์— ์ ‘๊ทผํ•  ๋•Œ๋Š” .(์ )(๋ฉค๋ฒ„ ์—ฐ์‚ฐ์ž)์„ ์‚ฌ์šฉ
๊ตฌ์กฐ์ฒด๋ณ€์ˆ˜์ด๋ฆ„.๊ตฌ์กฐ์ฒด๋ฉค๋ฒ„์ด๋ฆ„
#include <stdio.h>
#include <string.h>

struct student
{
	int number;
    char name[20];
    char major[20];
    double grade;
};

int main(void)
{
	struct student student1;
    
    // .(์ )์œผ๋กœ ๊ตฌ์กฐ์ฒด ๋ฉค๋ฒ„์— ์ ‘๊ทผํ•˜์—ฌ ๊ฐ’ ํ• ๋‹น
    student1.number = 01;
    strcpy(student1.name, "๊น€์œ ๋นˆ");
    strcpy(student1.major, "์‚ฌ์ด๋ฒ„๋ณด์•ˆํ•™๊ณผ");
    student1.grade = 3.5;
    
    // .(์ )์œผ๋กœ ๊ตฌ์กฐ์ฒด ๋ฉค๋ฒ„์— ์ ‘๊ทผํ•˜์—ฌ ๊ฐ’ ์ถœ๋ ฅ
    printf("ํ•™๋ฒˆ: %d\n", student1.number);
    printf("์ด๋ฆ„: %s\n", student1.name);
    printf("์ „๊ณต: %s\n", student1.major);
    printf("ํ•™์ : %.2f\n", student1.grade);
    
    return 0;
}

์‹คํ–‰๊ฒฐ๊ณผ
ํ•™๋ฒˆ: 01
์ด๋ฆ„: ๊น€์œ ๋นˆ
์ „๊ณต: ์‚ฌ์ด๋ฒ„๋ณด์•ˆํ•™๊ณผ
ํ•™์ : 3.50

โ˜… name๊ณผ major๋Š” ๋ฐฐ์—ด์ด๊ธฐ ๋•Œ๋ฌธ์— ๋ฌธ์ž์—ด ์ €์žฅ์„ ์œ„ํ•ด strcpy ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

strcpy(์ €์žฅ๋  ๋ฐฐ์—ด๋ช…, ์ €์žฅํ•  ๋ฌธ์ž์—ด)


๊ตฌ์กฐ์ฒด ๋ณ€์ˆ˜ ์„ ์–ธ+์ดˆ๊ธฐํ™”

  • ๊ตฌ์กฐ์ฒด ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•˜๋Š” ๋™์‹œ์— ๊ฐ’์„ ์ดˆ๊ธฐํ™”ํ•˜๋ ค๋ฉด { } ์ค‘๊ด„ํ˜ธ ์•ˆ์— .(์ )(๋ฉค๋ฒ„ ์—ฐ์‚ฐ์ž)๊ณผ ๋ฉค๋ฒ„ ์ด๋ฆ„์„ ์ ๊ณ  ๊ฐ’์„ ํ• ๋‹นํ•œ๋‹ค.
  • ์ด ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•˜๋ฉด ์›ํ•˜๋Š” ๋ฉค๋ฒ„ ๋ณ€์ˆ˜๋งŒ์„ ์ดˆ๊ธฐํ™”ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ๋ฉค๋ฒ„ ๋ณ€์ˆ˜๊ฐ€ ์ •์˜๋œ ์ˆœ์„œ์™€ ์ดˆ๊ธฐํ™” ์ˆœ์„œ๋Š” ์ƒ๊ด€์—†์œผ๋ฉฐ, ์ดˆ๊ธฐํ™”ํ•˜์ง€ ์•Š์€ ๋ฉค๋ฒ„ ๋ณ€์ˆ˜๋Š” 0์œผ๋กœ ์ดˆ๊ธฐํ™”๋œ๋‹ค.
struct ๊ตฌ์กฐ์ฒด์ด๋ฆ„ ๊ตฌ์กฐ์ฒด๋ณ€์ˆ˜์ด๋ฆ„ = { .๋ฉค๋ฒ„์ด๋ฆ„1 = ๊ฐ’1, .๋ฉค๋ฒ„์ด๋ฅธ2 = ๊ฐ’2 .. }
struct student student1 = { .ํ•™๋ฒˆ = 220101, .์ด๋ฆ„ = "๊น€์œ ๋นˆ', .์ „๊ณต = "์‚ฌ์ด๋ฒ„๋ณด์•ˆํ•™๊ณผ", .ํ•™์  = 3.5};

 

  • ๋ฉค๋ฒ„ ์ด๋ฆ„๊ณผ ํ• ๋‹น ์—ฐ์‚ฐ์ž ์—†์ด ๊ฐ’๋งŒ ,(์ฝค๋งˆ)๋กœ ๊ตฌ๋ถ„ํ•˜์—ฌ ๋‚˜์—ดํ•ด๋„ ๋œ๋‹ค.
  • ๋‹จ, ์ด ๋•Œ๋Š” ๊ตฌ์กฐ์ฒด ๋ฉค๋ฒ„๊ฐ€ ์„ ์–ธ๋œ ์ˆœ์„œ๋Œ€๋กœ ๋„ฃ๊ณ , ๊ฐ ๋ฉค๋ฒ„์˜ ์ž๋ฃŒํ˜•์— ๋งž๊ฒŒ ๋„ฃ๋Š”๋‹ค.
  • ์ฆ‰, ์ฒ˜์Œ๋ถ€ํ„ฐ ์ˆœ์„œ๋Œ€๋กœ ๊ฐ’์„ ์ฑ„์›Œ ๋„ฃ์–ด์•ผ ํ•˜๋ฉฐ ์ค‘๊ฐ„์— ์žˆ๋Š” ๋ฉค๋ฒ„๋งŒ ๊ฐ’์„ ํ• ๋‹นํ•˜๊ฑฐ๋‚˜, ์ค‘๊ฐ„์— ์žˆ๋Š” ๋ฉค๋ฒ„๋งŒ ์ƒ๋žตํ•  ์ˆ˜๋Š” ์—†๋‹ค.
struct ๊ตฌ์กฐ์ฒด์ด๋ฆ„ ๊ตฌ์กฐ์ฒด๋ณ€์ˆ˜์ด๋ฆ„ = { ๊ฐ’1, ๊ฐ’2, ... }
struct student student1 = { 220101, "๊น€์œ ๋นˆ", "์‚ฌ์ด๋ฒ„๋ณด์•ˆํ•™๊ณผ", 3.5 };

๊ตฌ์กฐ์ฒด์™€ ๋ฐฐ์—ด

  • ๊ตฌ์กฐ์ฒด ๋ฐฐ์—ด์€ ๋ณ€์ˆ˜ ์ด๋ฆ„ ๋’ค์— [ ] ๋Œ€๊ด„ํ˜ธ๋ฅผ ๋ถ™์ธ ๋’ค ํฌ๊ธฐ๋ฅผ ์„ค์ •ํ•œ๋‹ค.
struct ๊ตฌ์กฐ์ฒด์ด๋ฆ„ ๋ณ€์ˆ˜์ด๋ฆ„[ํฌ๊ธฐ];

 

  • ๊ตฌ์กฐ์ฒด ๋ฐฐ์—ด์—์„œ ๊ฐ ์š”์†Œ์— ์ ‘๊ทผํ•˜๋ ค๋ฉด ๋ฐฐ์—ด ๋’ค์— ๋Œ€๊ด„ํ˜ธ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉฐ ๋Œ€๊ด„ํ˜ธ ์•ˆ์— ์ธ๋ฑ์Šค๋ฅผ ์ง€์ •ํ•ด ์ค€๋‹ค.
  • ์—ฌ๊ธฐ์„œ ๋ฉค๋ฒ„์— ์ ‘๊ทผํ•˜๋ ค๋ฉด .(์ )(๋ฉค๋ฒ„ ์—ฐ์‚ฐ์ž)์„ ์‚ฌ์šฉํ•œ๋‹ค.
#include <stdio.h>
#include <string.h>

struct student
{
	int number;
    char name[20];
    char major[20];
    double grade;
};

int main(void)
{
	struct student arr[3];	// ํฌ๊ธฐ๊ฐ€ 3์ธ ๊ตฌ์กฐ์ฒด ๋ฐฐ์—ด ์ƒ์„ฑ
    int i;
    
    // ์ธ๋ฑ์Šค๋กœ ์š”์†Œ์— ์ ‘๊ทผํ•œ ๋’ค .(์ )์œผ๋กœ ๋ฉค๋ฒ„์— ์ ‘๊ทผ
    arr[0].number = 21012345;
    strcpy(arr[0].name, "๊น€์„ธ์ข…");
    strcpy(arr[0].major, "์ปดํ“จํ„ฐ๊ณตํ•™๊ณผ");
    arr[0].grade = 3.5;
    
    arr[1].number = 21012346;
    strcpy(arr[1].name, "์ด์„ธ์ข…");
    strcpy(arr[1].major, "์†Œํ”„ํŠธ์›จ์–ดํ•™๊ณผ");
    arr[1].grade = 2.5;
    
    arr[2].number = 21012347;
    strcpy(arr[2].name, "๋ฐ•์„ธ์ข…");
    strcpy(arr[2].major, "๋ฐ์ดํ„ฐ์‚ฌ์ด์–ธ์Šคํ•™๊ณผ");
    arr[2].grade = 4.5;
    
    for(i=0; i<3; i++){
    	printf("ํ•™๋ฒˆ : %d\n", arr[i].number);
        printf("์ด๋ฆ„ : %s\n", arr[i].name);
        printf("์ „๊ณต : %s\n", arr[i].major);
        printf("ํ•™์  : %.2f\n", arr[i].grade);
        printf("\n");
    }
    return 0;
}
์‹คํ–‰๊ฒฐ๊ณผ
ํ•™๋ฒˆ : 21012345
์ด๋ฆ„ : ๊น€์„ธ์ข…
์ „๊ณต : ์ปดํ“จํ„ฐ๊ณตํ•™๊ณผ
ํ•™์  : 3.50

ํ•™๋ฒˆ : 21012346
์ด๋ฆ„ : ์ด์„ธ์ข…
์ „๊ณต : ์†Œํ”„ํŠธ์›จ์–ดํ•™๊ณผ
ํ•™์  : 2.50

ํ•™๋ฒˆ : 21012347
์ด๋ฆ„ : ๋ฐ•์„ธ์ข…
์ „๊ณต : ๋ฐ์ดํ„ฐ์‚ฌ์ด์–ธ์Šคํ•™๊ณผ
ํ•™์  : 4.50


๊ตฌ์กฐ์ฒด ๋ฐฐ์—ด์„ ์„ ์–ธํ•˜๋ฉด์„œ ์ดˆ๊ธฐํ™”ํ•˜๋ ค๋ฉด { } ์ค‘๊ด„ํ˜ธ ์•ˆ์—  { } ์ค‘๊ด„ํ˜ธ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

 

struct ๊ตฌ์กฐ์ฒด์ด๋ฆ„ ๋ณ€์ˆ˜์ด๋ฆ„[ํฌ๊ธฐ] = {{ .๋ฉค๋ฒ„์ด๋ฆ„1 = ๊ฐ’1, .๋ฉค๋ฒ„์ด๋ฆ„2 = ๊ฐ’2 ...},
				{ .๋ฉค๋ฒ„์ด๋ฆ„1 = ๊ฐ’3, .๋ฉค๋ฒ„์ด๋ฆ„2 = ๊ฐ’4 ...}
                                    ...
                                  };
struct student arr[3] = { { .ํ•™๋ฒˆ = 21012345, .์ด๋ฆ„ = "๊น€์„ธ์ข…", .์ „๊ณต = "์ปดํ“จํ„ฐ๊ณตํ•™๊ณผ", .ํ•™์  = 3.5 },
                          { .ํ•™๋ฒˆ = 21012346, .์ด๋ฆ„ = "์ด์„ธ์ข…", .์ „๊ณต = "์†Œํ”„ํŠธ์›จ์–ดํ•™๊ณผ", .ํ•™์  = 2.5 },
                          { .ํ•™๋ฒˆ = 21012347, .์ด๋ฆ„ = "๋ฐ•์„ธ์ข…", .์ „๊ณต = "๋ฐ์ดํ„ฐ์‚ฌ์ด์–ธ์Šคํ•™๊ณผ", .ํ•™์  = 4.5 } }

 

struct ๊ตฌ์กฐ์ฒด์ด๋ฆ„ ๋ณ€์ˆ˜์ด๋ฆ„[ํฌ๊ธฐ] = { { ๊ฐ’1, ๊ฐ’2 ... }, { ๊ฐ’3, ๊ฐ’4 ... }, ... };
struct student arr[3] = { { 21012345, "๊น€์„ธ์ข…", "์ปดํ“จํ„ฐ๊ณตํ•™๊ณผ", 3.5 },
                          { 21012346, "์ด์„ธ์ข…", "์†Œํ”„ํŠธ์›จ์–ดํ•™๊ณผ", 2.5 },
                          { 21012347, "๋ฐ•์„ธ์ข…", "๋ฐ์ดํ„ฐ์‚ฌ์ด์–ธ์Šคํ•™๊ณผ", 4.5 } }

๊ตฌ์กฐ์ฒด์™€ ํฌ์ธํ„ฐ

  • ํฌ์ธํ„ฐ๋Š” ๋ฉ”๋ชจ๋ฆฌ์˜ ์ฃผ์†Œ๊ฐ’์„ ์ €์žฅํ•˜๋Š” ๋ณ€์ˆ˜์ด๋‹ค.
  • ๊ตฌ์กฐ์ฒด๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” ํฌ์ธํ„ฐ๋ฅผ ๊ตฌ์กฐ์ฒด ํฌ์ธํ„ฐ๋ผ๊ณ  ํ•œ๋‹ค.
  • ๋ณ€์ˆ˜์ด๋ฆ„ ์•ž์— * ์„ ๋ถ™์—ฌ ์„ ์–ธ
struct ๊ตฌ์กฐ์ฒด์ด๋ฆ„ * ๊ตฌ์กฐ์ฒดํฌ์ธํ„ฐ;

๊ตฌ์กฐ์ฒด ํฌ์ธํ„ฐ๋ฅผ ์ด์šฉํ•œ ๊ตฌ์กฐ์ฒด์˜ ๋ฉค๋ฒ„์— ์ ‘๊ทผํ•˜๋Š” ๋ฐฉ๋ฒ•

1. *(์ฐธ์กฐ ์—ฐ์‚ฐ์ž)๋ฅผ ์ด์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•

(*๊ตฌ์กฐ์ฒดํฌ์ธํ„ฐ).๋ฉค๋ฒ„๋ณ€์ˆ˜์ด๋ฆ„

 

2. ->(ํ™”์‚ดํ‘œ ์—ฐ์‚ฐ์ž)๋ฅผ ์ด์šฉํ•˜๋Š” ๋ฐฉ๋ฒ• ( ** ๋” ๋งŽ์ด ์‚ฌ์šฉ )

๊ตฌ์กฐ์ฒดํฌ์ธํ„ฐ -> ๋ฉค๋ฒ„๋ณ€์ˆ˜์ด๋ฆ„

#include <stdio.h>

struct student
{
	int number;
    char name[20];
    char major[20];
    double grade;
};

int main(void)
{
	struct student student1 = { 21012345, "๊น€์„ธ์ข…", "์ปดํ“จํ„ฐ๊ณตํ•™๊ณผ", 3.5 };
    struct student * ptr = &student1;
    
    printf("ํ•™๋ฒˆ : %d\n", (*ptr).number);
    printf("์ด๋ฆ„ : %s\n", (*ptr).name);
    printf("์ „๊ณต : %s\n", (*ptr).major);
    printf("ํ•™์  : %.2f\n", (*ptr).grade);
    
    return 0;
}
์‹คํ–‰๊ฒฐ๊ณผ
ํ•™๋ฒˆ : 21012345
์ด๋ฆ„ : ๊น€์„ธ์ข…
์ „๊ณต : ์ปดํ“จํ„ฐ๊ณตํ•™๊ณผ
ํ•™์  : 3.50

 

#include <stdio.h>

struct student
{
	int number;
    char name[20];
    char major[20];
    double grade;
};

int main(void)
{
	struct student student1 = { 21012345, "๊น€์„ธ์ข…", "์ปดํ“จํ„ฐ๊ณตํ•™๊ณผ", 3.5 };
    struct student * ptr = &student1;
    
    printf("ํ•™๋ฒˆ : %d\n", ptr -> number);
    printf("์ด๋ฆ„ : %s\n", ptr -> name);
    printf("์ „๊ณต : %s\n", ptr -> major);
    printf("ํ•™์  : %.2f\n", ptr -> grade);
    
    return 0;
}
์‹คํ–‰๊ฒฐ๊ณผ
ํ•™๋ฒˆ : 21012345
์ด๋ฆ„ : ๊น€์„ธ์ข…
์ „๊ณต : ์ปดํ“จํ„ฐ๊ณตํ•™๊ณผ
ํ•™์  : 3.50

 

 

  • studentํ˜• ํฌ์ธํ„ฐ ๋ณ€์ˆ˜ ptr์ด ๊ตฌ์กฐ์ฒด ๋ณ€์ˆ˜ student1์„ ๊ฐ€๋ฆฌํ‚จ๋‹ค.
  • studentํ˜• ํฌ์ธํ„ฐ ๋ณ€์ˆ˜ ptr์„ ์ด์šฉํ•ด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ตฌ์กฐ์ฒด ๋ณ€์ˆ˜ student1์— ์ ‘๊ทผํ•œ๋‹ค.

typedef

  • typedef ์„ ์–ธ์€ ๊ธฐ์กด์— ์กด์žฌํ•˜๋Š” ์ž๋ฃŒํ˜•์˜ ์ด๋ฆ„์— ์ƒˆ ์ด๋ฆ„์„ ๋ถ€์—ฌํ•œ๋‹ค.
  • typedef ์„ ์–ธ์„ ์‚ฌ์šฉํ•˜์—ฌ ์ด๋ฏธ ์ •์˜๋œ ํ˜•์‹์ด๋‚˜ ์‚ฌ์šฉ์ž๊ฐ€ ์„ ์–ธํ•œ ํ˜•์‹์— ๋Œ€ํ•œ ๋ณด๋‹ค ์งง๊ฑฐ๋‚˜ ์˜๋ฏธ ์žˆ๋Š” ์ด๋ฆ„์„ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.
typedef  ๊ธฐ์กด_์ž๋ฃŒํ˜•  ์ƒˆ๋กญ๊ฒŒ_์„ ์–ธํ• _์ž๋ฃŒํ˜•;

 

typedef unsigned int myint;

unsigned int num;
// ์œ„ ์ฝ”๋“œ์™€ ์•„๋ž˜ ์ฝ”๋“œ๋Š” ๊ฐ™์€ ์˜๋ฏธ์ž…๋‹ˆ๋‹ค.
myint num;

  • typedef๋ฅผ ์ด์šฉํ•˜๋ฉด ๊ตฌ์กฐ์ฒด ๋ณ€์ˆ˜๋ฅผ ๋งŒ๋“ค ๋•Œ struct ์„ ์–ธ์„ ์ƒ๋žตํ•  ์ˆ˜ ์žˆ๋‹ค.
  • typedef๋กœ ๊ตฌ์กฐ์ฒด๋ฅผ ์ •์˜ํ•˜๋ฉด์„œ ๋ณ„์นญ์„ ์ง€์ •ํ•ด ์ฃผ๋ฉด ๋œ๋‹ค.
struct student
{
    int number;
    char name[20];
    char major[20];
    double grade;
};

๊ตฌ์กฐ์ฒด ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธ๊ณผ ์ดˆ๊ธฐํ™”ํ•ด์•ผ ํ•œ๋‹ค.
struct student student1 = { 21012345, "๊น€์„ธ์ข…", "์ปดํ“จํ„ฐ๊ณตํ•™๊ณผ", 3.5 };

typedef ์„ ์–ธ์„ ํ†ตํ•ด struct student๋ฅผ ๋Œ€์‹ ํ•  ์ˆ˜ ์žˆ๋„๋ก Student๋ผ๋Š” ์ด๋ฆ„์„ ์ •์˜ํ•  ์ˆ˜ ์žˆ๋‹ค.
typedef struct student Student;

struct ์„ ์–ธ์„ ์ƒ๋žตํ•˜๊ณ  ๊ตฌ์กฐ์ฒด ๋ณ€์ˆ˜ ์„ ์–ธ๊ณผ ์ดˆ๊ธฐํ™”๋ฅผ ํ•  ์ˆ˜ ์žˆ๋‹ค.
Student student1 = { 21012345, "๊น€์„ธ์ข…", "์ปดํ“จํ„ฐ๊ณตํ•™๊ณผ", 3.5 };

 

 

typedef๋กœ ๊ตฌ์กฐ์ฒด๋ฅผ ์ •์˜ํ•˜๋ฉด์„œ ๋ณ„์นญ์„ ์ง€์ •

typedef struct ๊ตฌ์กฐ์ฒด์ด๋ฆ„
{
   ๋ฉค๋ฒ„๋ณ€์ˆ˜1์ž๋ฃŒํ˜• ๋ฉค๋ฒ„๋ณ€์ˆ˜1์ด๋ฆ„;
   ๋ฉค๋ฒ„๋ณ€์ˆ˜2์ž๋ฃŒํ˜• ๋ฉค๋ฒ„๋ณ€์ˆ˜2์ด๋ฆ„;
   ๋ฉค๋ฒ„๋ณ€์ˆ˜3์ž๋ฃŒํ˜• ๋ฉค๋ฒ„๋ณ€์ˆ˜3์ด๋ฆ„;
    ...
} ๊ตฌ์กฐ์ฒด๋ณ„์นญ;

 

 

  • ๊ตฌ์กฐ์ฒด ์ด๋ฆ„ ์ƒ๋žตํ•˜๋Š” ๋ฐฉ๋ฒ•๋„ ์žˆ๋‹ค. ๋‹จ, ์˜ค๋ฅธ์ชฝ๊ณผ ๊ฐ™์ด ๊ตฌ์กฐ์ฒด์˜ ์ด๋ฆ„์„ ์ƒ๋žตํ•˜๋ฉด struct ์„ ์–ธ์„ ์ถ”๊ฐ€ํ•˜๋ฉด์„œ ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•  ์ˆ˜ ์—†๋‹ค.

Student student1;   // ๊ฐ€๋Šฅํ•œ ์„ ์–ธ
struct student student1;  // ๋ถˆ๊ฐ€๋Šฅํ•œ ์„ ์–ธ

๊ณต์šฉ์ฒด

  • ๊ณต์šฉ์ฒด๋Š” union ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์„ ์–ธํ•˜๋ฉฐ, ๊ตฌ์กฐ์ฒด์™€ ๋น„์Šทํ•˜๋‹ค.
  • ๋ชจ๋“  ๋ฉค๋ฒ„ ๋ณ€์ˆ˜๊ฐ€ ํ•˜๋‚˜์˜ ๋ฉ”๋ชจ๋ฆฌ ๊ณต๊ฐ„์„ '๊ณต์œ 'ํ•œ๋‹ค๋Š” ์ ๋งŒ์ด ๋‹ค๋ฅด๋‹ค.
  • ๋ชจ๋“  ๋ฉค๋ฒ„ ๋ณ€์ˆ˜๊ฐ€ ๊ฐ™์€ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ๊ณต์œ ํ•˜๋ฏ€๋กœ, ๊ณต์šฉ์ฒด๋Š” ํ•œ ๋ฒˆ์— ํ•˜๋‚˜์˜ ๋ฉค๋ฒ„ ๋ณ€์ˆ˜๋งŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ๊ณต์šฉ์ฒด๋ฅผ ์ •์˜ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ํ‚ค์›Œ๋“œ struct ๋Œ€์‹  union์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด๋‹ค.
union ๊ณต์šฉ์ฒด์ด๋ฆ„
{
   ๋ฉค๋ฒ„๋ณ€์ˆ˜1์ž๋ฃŒํ˜• ๋ฉค๋ฒ„๋ณ€์ˆ˜1์ด๋ฆ„;  
   ๋ฉค๋ฒ„๋ณ€์ˆ˜2์ž๋ฃŒํ˜• ๋ฉค๋ฒ„๋ณ€์ˆ˜2์ด๋ฆ„;  
    ...
};

#include <stdio.h>

typedef struct snum		// ๊ตฌ์กฐ์ฒด ์„ ์–ธ
{
	int a;
    int b;
    double c;
} Snum;

typedef union unum		// ๊ณต์šฉ์ฒด ์„ ์–ธ
{
	int a;
    int b;
    double c;
} Unum;

int main(void)
{
	Snum sn;
    Unum un;
    printf("%p %p %p\n", &sn.a, &sn.b, &sn.c);
    printf("%p %p %p\n", &un.a, &un.b, &un.c);
    printf("%d %d\n", sizeof(Snum), sizeof(Unum));
    
    return 0;
}
์‹คํ–‰๊ฒฐ๊ณผ
00DBFE50 00DBFE54 00DBFE58
00DBFE40 00DBFE40 00DBFE40
16 8

โ–ถ Snumํ˜• ๋ณ€์ˆ˜๋ฅผ ๊ตฌ์„ฑํ•˜๋Š” ๋ฉค๋ฒ„๋“ค์˜ ์ฃผ์†Œ๊ฐ’์€ ๋‹ค๋ฅธ๋ฐ Unumํ˜• ๋ณ€์ˆ˜๋ฅผ ๊ตฌ์„ฑํ•˜๋Š” ๋ฉค๋ฒ„๋“ค์˜ ์ฃผ์†Œ๊ฐ’์€ ๋™์ผํ•˜๋‹ค๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

sizeof ์—ฐ์‚ฐ์ž๋กœ ์ž๋ฃŒํ˜•์˜ ํฌ๊ธฐ๋ฅผ ๊ตฌํ•˜๋ฉด 16๊ณผ 8์ด ์ถœ๋ ฅ๋œ๋‹ค.

int๋Š” 4๋ฐ”์ดํŠธ, double์€ 8๋ฐ”์ดํŠธ์ด๋‹ค.

์—ฌ๊ธฐ์„œ 16์ธ ๋ชจ๋“  ๋ฉค๋ฒ„ ํฌ๊ธฐ๋ฅผ ํ•ฉํ•œ ๊ฒฐ๊ณผ 16์ด๊ณ , 8์€ ๋ฉค๋ฒ„์ค‘์—์„œ๊ฐ€์žฅ ํฌ๊ธฐ๊ฐ€ ํฐ double์˜ ํฌ๊ธฐ๋งŒ ๊ณ„์‚ฐ๋œ ๊ฒฐ๊ณผ์ด๋‹ค.


#include <stdio.h>

typedef union unum		// ๊ณต์šฉ์ฒด ์„ ์–ธ
{
	int a;
    int b;
    double c;
} Unum;

int main(void)
{
	Unum un;		// 8๋ฐ”์ดํŠธ ๋ฉ”๋ชจ๋ฆฌ ํ• ๋‹น
    
    un.a = 10;
    printf("%d\n", un.b);	// b ๋ฉค๋ฒ„ ์ถœ๋ ฅ
    
    un.c = 11.1;		// c ๋ฉค๋ฒ„์— ๊ฐ’ ๋Œ€์ž…
    printf("%d\n", un.a);	// a ๋ฉค๋ฒ„ ์ถœ๋ ฅ
    printf("%d\n", un.b);	// b ๋ฉค๋ฒ„ ์ถœ๋ ฅ
    printf("%f\n", un.c);	// c ๋ฉค๋ฒ„ ์ถœ๋ ฅ
    
    return 0;
}
์‹คํ–‰๊ฒฐ๊ณผ
10
858993459
858993459
11.100000

  โ–ถ ์ฒ˜์Œ์— a(์ƒ์œ„ 4๋ฐ”์ดํŠธ์˜ ๋ฉ”๋ชจ๋ฆฌ ๊ณต๊ฐ„)์— 10์„ ์ €์žฅํ–ˆ๋‹ค.

b๋Š” intํ˜• ๋ณ€์ˆ˜์ด๋ฏ€๋กœ b ๋ฉค๋ฒ„์— ์ ‘๊ทผํ•  ๊ฒฝ์šฐ ์ƒ์œ„ 4๋ฐ”์ดํŠธ์˜ ๊ณต๊ฐ„์„ ์ฐธ์กฐํ•˜๊ฒŒ ๋œ๋‹ค.

๊ทธ๋Ÿฐ๋ฐ ์•ž์„œ ์ด ๊ณต๊ฐ„์— 10์„ ์ €์žฅํ–ˆ์œผ๋ฏ€๋กœ 10์ด ์ถœ๋ ฅ๋œ๋‹ค.

c์— ์‹ค์ˆ˜๋ฅผ ์ €์žฅํ•จ์œผ๋กœ์จ ๊ฒฐ๊ณผ์ ์œผ๋กœ ์ƒ์œ„ 4๋ฐ”์ดํŠธ์— ์ €์žฅ๋œ ๊ฐ’์„ ๋ฎ์–ด ์จ๋ฒ„๋ฆฐ๋‹ค.

์‹ค์ˆ˜๋ฅผ ์ €์žฅํ•˜๋ฉด์„œ ๋ฎ์–ด์จ๋ฒ„๋ ธ๊ธฐ ๋•Œ๋ฌธ์—, a, b(์ƒ์œ„ 4๋ฐ”์ดํŠธ)๋ฅผ ์ฝ์–ด์„œ ์ถœ๋ ฅํ•˜๋ฉด ์•Œ ์ˆ˜ ์—†๋Š” ๊ฐ’์ด ์ถœ๋ ฅ๋œ๋‹ค.

  ๊ตฌ์กฐ์ฒด(struct) ๊ณต์šฉ์ฒด(union)
๋ฉ”๋ชจ๋ฆฌ ๊ตฌ์กฐ์ฒด์˜ ๊ฐ๊ฐ์˜ ๋ฉค๋ฒ„๋“ค์€ ๋ชจ๋‘ ๋‹ค๋ฅธ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ๊ฐ–์Šต๋‹ˆ๋‹ค.
๋”ฐ๋ผ์„œ ๊ฐ๊ฐ ๋ฉ”๋ชจ๋ฆฌ์˜ ์‹œ์ž‘ ์ฃผ์†Œ๊ฐ€ ๋‹ค๋ฆ…๋‹ˆ๋‹ค.
๊ณต์šฉ์ฒด์˜ ๋ชจ๋“  ๋ฉค๋ฒ„๋“ค์€ ๋ชจ๋‘ ๊ฐ™์€ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ๊ณต์œ ํ•ฉ๋‹ˆ๋‹ค.
๋”ฐ๋ผ์„œ ๊ฐ๊ฐ์˜ ๋ฉค๋ฒ„๋“ค์€ ์‹œ์ž‘ ์ฃผ์†Œ๊ฐ€ ๋ชจ๋‘ ๋™์ผํ•ฉ๋‹ˆ๋‹ค.
์ดˆ๊ธฐํ™” ๊ตฌ์กฐ์ฒด ๋ฉค๋ฒ„๋“ค์„ ํ•œ ๋ฒˆ์— ์ดˆ๊ธฐํ™”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ณต์šฉ์ฒด์˜ ์ดˆ๊ธฐํ™”๋Š” ์ฒซ ๋ฒˆ์งธ ๋ฉค๋ฒ„์—์„œ๋งŒ ์ดˆ๊ธฐํ™”๋ฅผ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
์ฒซ๋ฒˆ์งธ ๋ฉค๋ฒ„๊ฐ€ ์•„๋‹Œ ๋ฉค๋ฒ„๋ฅผ ์ดˆ๊ธฐํ™”ํ•  ๋•Œ๋Š” .(๋ฉค๋ฒ„ ์ ‘๊ทผ ์—ฐ์‚ฐ์ž)๋กœ ๋ฉค๋ฒ„๋ฅผ ์ง์ ‘ ์ง€์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
์ „์ฒด ํฌ๊ธฐ ๋ชจ๋“  ๊ตฌ์กฐ์ฒด ๋ฉค๋ฒ„ ํฌ๊ธฐ์˜ ํ•ฉ์ž…๋‹ˆ๋‹ค. ๊ฐ€์žฅ ํฐ ๋ฉค๋ฒ„์˜ ํฌ๊ธฐ์ž…๋‹ˆ๋‹ค.
์ ‘๊ทผ ๊ตฌ์กฐ์ฒด์˜ ๋ชจ๋“  ๋ฉค๋ฒ„์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•˜๋‚˜์˜ ๋ฉค๋ฒ„์—๋งŒ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 


์—ด๊ฑฐํ˜•

  • ์—ด๊ฑฐํ˜•์€ enum ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์„ ์–ธํ•œ๋‹ค.
  • enumeration์˜ ์•ฝ์ž๋กœ์„œ, ์—ด๊ฑฐํ•œ๋‹ค๋Š” ์˜๋ฏธ๋ฅผ ๊ฐ€์ง„๋‹ค.
  • ์ดˆ๊ธฐํ™”๋ฅผ ํ•˜์ง€ ์•Š์œผ๋ฉด ์—ด๊ฑฐ๋œ ์ˆœ์„œ์— ๋”ฐ๋ผ 0๋ถ€ํ„ฐ ์ฐจ๋ก€๋Œ€๋กœ ์ฆ๊ฐ€ํ•œ๋‹ค
enum ์—ด๊ฑฐํ˜•์ด๋ฆ„
{
    ์ƒ์ˆ˜๋ช…1, ์ƒ์ˆ˜๋ช…2, ...
};
enum days_of_week
{
	mon, tue, wed, thu, fri, sat, sun	// ์—ด๊ฑฐํ˜• ์„ ์–ธ
};

์œ„์™€ ๊ฐ™์€ ํ˜•ํƒœ์ด๋ฉด mon์€ 0, tue๋Š” 1, wed๋Š” 2, thu๋Š” 3, fri๋Š” 4, sat๋Š” 5, sun๋Š” 6

enum days_of_week
{
	mon=3, tue, wed, thu=50, fri, sat, sun	// ์—ด๊ฑฐํ˜• ์„ ์–ธ
};
์œ„์™€ ๊ฐ™์€ ํ˜•ํƒœ์ด๋ฉด mon๋Š” 3, tue๋Š” 4, wed๋Š” 5, thu๋Š” 50, fri๋Š” 51, sat๋Š” 52, sun๋Š” 53

  • ์ •์˜๋œ ์—ด๊ฑฐํ˜•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์—ด๊ฑฐํ˜• ๋ณ€์ˆ˜๋กœ ์„ ์–ธํ•˜์—ฌ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ์—ด๊ฑฐํ˜• ๋ณ€์ˆ˜๋Š” intํ˜•๊ณผ ๊ฐ™์€ ํฌ๊ธฐ๋กœ ์ •์ˆ˜๋ฅผ ์ €์žฅํ•  ์ˆ˜ ์žˆ๋Š” ๊ณต๊ฐ„์ด ํ• ๋‹น๋œ๋‹ค.
  • ๋˜ํ•œ ์—ด๊ฑฐํ˜• ๋ฉค๋ฒ„๋ฅผ ์ €์žฅํ•˜๋Š” ์šฉ๋„๋กœ ์“ฐ์ธ๋‹ค.
enum ์—ด๊ฑฐํ˜•์ด๋ฆ„ ์—ด๊ฑฐํ˜•๋ณ€์ˆ˜์ด๋ฆ„;
enum days_of_week day;   // ์—ด๊ฑฐํ˜• ๋ณ€์ˆ˜ ์„ ์–ธ
#include <stdio.h>

enum days_of_week
{
	mon, tue, wed, thu, fri, sad, sun	// ์—ด๊ฑฐํ˜• ์„ ์–ธ
};

int main(void)
{
    enum days_of_week day;	// ์—ด๊ฑฐํ˜• ๋ณ€์ˆ˜ ์„ ์–ธ
    
    day = fri;	// ์—ด๊ฑฐ ๋ฉค๋ฒ„์˜ ๊ฐ’ ๋Œ€์ž…
    
    switch (day)	// ์—ด๊ฑฐ ๋ฉค๋ฒ„ ํŒ๋‹จ
    {
    case mon:
    	printf("Hate Monday\n");
        break;
    case tue:
        printf("Annoy Tuesday\n"); 
        break;
    case wed:
        printf("Ignore Wednesday\n"); 
        break;
    case thu:
        printf("Smile Thursday\n"); 
        break;
    case fri:
        printf("Love Friday\n"); 
        break;
    case sat:
        printf("Enjoy Saturday\n"); 
        break;
    case sun:
        printf("Lazy Sunday\n"); 
        break;
    }
    return 0;
}
์‹คํ–‰๊ฒฐ๊ณผ
Love Friday

โ–ถ ์œ„์ชฝ ์˜ˆ์ œ์˜ case ๋ ˆ์ด๋ธ”์—์„œ ์‚ฌ์šฉํ•œ ์ƒ์ˆ˜ mon, tue, wed...๋ฅผ ๋Œ€์‹ ํ•ด์„œ 0,1,2...๋ฅผ ์‚ฝ์ž…ํ•ด๋„ ๊ฒฐ๊ณผ๋Š” ๋™์ผํ•˜๋‹ค.

mon, tue, wed... ๊ฐ™์€ ์—ด๊ฑฐํ˜• ์ƒ์ˆ˜๋Š” intํ˜•์œผ๋กœ ํ‘œํ˜„๋˜๋Š” ์ƒ์ˆ˜์ด๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

์—ด๊ฑฐํ˜• ๋ฉค๋ฒ„๋Š” ์‚ฌ์‹ค ์ •์ˆ˜ ์ƒ์ˆ˜๋ฅผ ์จ์„œ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.

๊ทธ๋Ÿฌ๋‚˜ ์—ด๊ฑฐํ˜•์„ ์ •์˜ํ•˜๋ฉด ์š”์ผ ์ด๋ฆ„์„ ์ง์ ‘ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ํ›จ์”ฌ ์ฝ๊ธฐ ์‰ฌ์šด ์ฝ”๋“œ๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.

 

โ˜… struct์™€ ๋งŒ์ฐฌ๊ฐ€์ง€๋กœ, typedef๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ enum ํ‚ค์›Œ๋“œ๋ฅผ ์ƒ๋žตํ•  ์ˆ˜ ์žˆ๋‹ค.

 

 

์ถœ์ฒ˜: https://sejong-kr.libguides.com/c.php?g=942235&p=6822369

'ํ”„๋กœ๊ทธ๋ž˜๋ฐ > C์–ธ์–ด' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

ํŒŒ์ผ ์ž…์ถœ๋ ฅ  (0) 2024.08.29
ํฌ์ธํ„ฐ & ํ•จ์ˆ˜  (0) 2024.08.16
ํฌ์ธํ„ฐ & ๋ฐฐ์—ด  (0) 2024.08.16
ํฌ์ธํ„ฐ  (0) 2024.08.15
๋ฐฐ์—ด  (0) 2024.08.14

+ Recent posts