Skip to content

Files

Latest commit

 

History

History

Week2-StructArray,Union,Enum

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 

[2주차] 구조체 배열, 공용체, 열거형

🔑 key word : struct array union enum

2주차 온라인 저지 문제 풀이 zoom 녹화본

구조체 배열

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>

struct student { //구조체 정의
	unsigned int sid; //4 byte
	char name[20]; //20
	float grade; //4
};
void showStudentInfo(struct student _s) { //구조체 출력함수
	printf("%d  : %s  --> %.2f\n", _s.sid, _s.name, _s.grade);
}

int main()
{
	////////////// 구조체 배열 ////////////////
	struct student st_db[3] = { //구조체 초기화
		{1,"kim",3.8},
		{2,"lee",3.9},
		{3,"park",4.0}
	};
	
	printf("%d\n", sizeof(st_db)/sizeof(struct student));//구조체 배열의 갯수 (84/28)

	for (int i = 0; i < 3; i++) { //구조체 출력
		showStudentInfo(st_db[i]);
	}
    return 0;
}

구조체 사이즈는 멤버들 크기 합

image

공용체는 하나만 들어가게 됨

image

공용체 사이즈는 큰 멤버 따라가되 4바이트 단위

image
멤버로 찍어보면 13 바이트임

공용체를 초기화 할 때는 형태 같도록

image

공용체 변수 정의 (일회용임, 형태 정의 없애고 바로 변수명)

image

구조체 변수 정의

image

Enum(열거형)은 내부에서 숫자로 대입됨

image

Enum 시작숫자 지정 가능

image

Enum 내부숫자 비교 가능

image