24
13 주 주주 Structures and Unions

13 주 강의 Structures and Unions. Structures Structure – 기본 자료형들로 구성된 새로운 자료형 예 ) struct card { intpips; char suit; };

Embed Size (px)

Citation preview

Page 1: 13 주 강의 Structures and Unions. Structures Structure – 기본 자료형들로 구성된 새로운 자료형 예 ) struct card { intpips; char suit; };

13 주 강의

Structures and Unions

Page 2: 13 주 강의 Structures and Unions. Structures Structure – 기본 자료형들로 구성된 새로운 자료형 예 ) struct card { intpips; char suit; };

Structures

• Structure– 기본 자료형들로 구성된 새로운 자료형

• 예 ) struct card { int pips; char suit; };

Page 3: 13 주 강의 Structures and Unions. Structures Structure – 기본 자료형들로 구성된 새로운 자료형 예 ) struct card { intpips; char suit; };

Structures struct card { int pips; char suit; }; struct card c1, c2; struct card { int pips; char suit; } c1, c2;※ pips,suit 를 멤버로 가지는 card Type 의 c1,c2

선언

Page 4: 13 주 강의 Structures and Unions. Structures Structure – 기본 자료형들로 구성된 새로운 자료형 예 ) struct card { intpips; char suit; };

값 저장• c1.pips = 3;• c1.suit = ‘s’;• c2=c1; /* structure 전체를 복사 */• typedef struct card card;• card c3, c4, c5;

Page 5: 13 주 강의 Structures and Unions. Structures Structure – 기본 자료형들로 구성된 새로운 자료형 예 ) struct card { intpips; char suit; };

다른 예• struct fruit { char *name; int calories; }• struct vegetable { char *name; int calories; }• struct fruit a;• struct vegetable b;

Page 6: 13 주 강의 Structures and Unions. Structures Structure – 기본 자료형들로 구성된 새로운 자료형 예 ) struct card { intpips; char suit; };

예• struct card { int pips; char suit; } deck[52];• struct date { int day, month, year; char day_name[4]; /* Mon, Tue, .. */ char month_name[4]; /* Jan, … */ } yesterday, today, tomorrow;

Page 7: 13 주 강의 Structures and Unions. Structures Structure – 기본 자료형들로 구성된 새로운 자료형 예 ) struct card { intpips; char suit; };

Complex number

• typedef struct { float re; float im; } complex;• complex a, b, c[100];

Page 8: 13 주 강의 Structures and Unions. Structures Structure – 기본 자료형들로 구성된 새로운 자료형 예 ) struct card { intpips; char suit; };

Accessing members

• #define CLASS_SIZE 100 struct student { char *last name; int student_id; char grade; }; struct student tmp, class[CLASS_SIZE];• tmp.grade = ‘A’;• tmp.last_name = “Casanova”;• tmp.student_id = 910017;• class[i].grade = ‘F’;

Page 9: 13 주 강의 Structures and Unions. Structures Structure – 기본 자료형들로 구성된 새로운 자료형 예 ) struct card { intpips; char suit; };

예제 설명 및 ->

• Page 412 프로그램 설명• ->

– pointer_to_structure->member_name (*pointer_to_structure).member_name/* () 가 없으면 오류 */

• complex *a, *b, *c;– a->re = b-> re + c -> re;

Page 10: 13 주 강의 Structures and Unions. Structures Structure – 기본 자료형들로 구성된 새로운 자료형 예 ) struct card { intpips; char suit; };

Structures

• structure_declaration::=struct_specifier declarator_list;• struct_specifier::=struct tag_name struct tag_nameopt {{member_declaration}1+}• tag_name::=identifier• member_declaration::=type_specifier declarator_list;• declarator_list::=declarator{,declarator}0+

Page 11: 13 주 강의 Structures and Unions. Structures Structure – 기본 자료형들로 구성된 새로운 자료형 예 ) struct card { intpips; char suit; };

Member access operators

Declarations and assignments

struct student tmp, *p = &tmp;tmp.grade = ‘A’;tmp.last_name = “Casanava”;tmp.student_id = 910017;

Expression Equivalent expression

Conceptual value

tmp.grade p->grade A

tmp.last_name p->last_name Casanava

(*p).student_id p->student_id 910017

*p->last_name+1 (*p->last_name))+1 D

*(p->last_name+2) (p->last_name)[2] s

Page 12: 13 주 강의 Structures and Unions. Structures Structure – 기본 자료형들로 구성된 새로운 자료형 예 ) struct card { intpips; char suit; };

Operator Precedence and Associativity

Operator Associativity

() [] . -> ++(postfix) --(postfix) left to right

++(prefix) --(prefix) ! ~ sizeof(type) +(unary) -(unary) &(address) *(dereference)

right to left

* / % left to right

+ - left to right

<< >> left to right

< <= > >= left to right

== != left to right

& left to right

^ left to right

| left to right

&& left to right

|| left to right

?: right to left

= += -= /= %= >>= <<= &= ^= |=

right to left

,(comma operator) left to right

Page 13: 13 주 강의 Structures and Unions. Structures Structure – 기본 자료형들로 구성된 새로운 자료형 예 ) struct card { intpips; char suit; };

Using structures with functions

• typedef struct { char name[25]; int employee_id; struct dept deparment; struct home_address *a_ptr; double salary; …. } employee_data;

Page 14: 13 주 강의 Structures and Unions. Structures Structure – 기본 자료형들로 구성된 새로운 자료형 예 ) struct card { intpips; char suit; };

Cont.

• struct dept { char dept_name[25]; int dept_no; };• e.department.dept_no (e.department).dept

_no• employee_data e; e = update(e);

Page 15: 13 주 강의 Structures and Unions. Structures Structure – 기본 자료형들로 구성된 새로운 자료형 예 ) struct card { intpips; char suit; };

Initialization of Structures

• card c={13,’h’};• struct fruit frt={“plum”, 150);• struct home_address {

char *street;char *city_and_state;long zip_code;

} address = {“87 West Street”,”Aspen, Colorado”, 80526};

• struct home_address previous_address = {0};

Page 16: 13 주 강의 Structures and Unions. Structures Structure – 기본 자료형들로 구성된 새로운 자료형 예 ) struct card { intpips; char suit; };

Cont.

• complex[3][3] = { {{1.0, -0.1}, {2.0, 0.2},

{3.0,0.3}} {{4.0,-0.4}, {5.0,0.5}, {60.,0.6}} };

• Poker game 설명

Page 17: 13 주 강의 Structures and Unions. Structures Structure – 기본 자료형들로 구성된 새로운 자료형 예 ) struct card { intpips; char suit; };

UNION

• 구조체와 같은 구문 형식이지만 , 각 멤버들은 같은 기억 장소를 공유

• union int_ot_float { int i; float f; }; union int_or_float a,b,c;

Page 18: 13 주 강의 Structures and Unions. Structures Structure – 기본 자료형들로 구성된 새로운 자료형 예 ) struct card { intpips; char suit; };

예• union int_or_float n; n.i = 4444; n.f = 444.0; n.i 4444 n.f 0.6227370375e-41 n.f 4444.0 n.i 116672921

Page 19: 13 주 강의 Structures and Unions. Structures Structure – 기본 자료형들로 구성된 새로운 자료형 예 ) struct card { intpips; char suit; };

다른 예• struct flower { char *name; enum{red, white, blue} color; };• struct fruit {

char *name;int calories;

};

• struct vegetable {char *name;int calories;int cooking_time;

};• struct flower_fruit_or_vegetable {

struct flower flw;struct fruit frt;struct vegetable veg;

};

• union flower_fruit_or_vegetable ffv;

•ffv.veg.cooking_time = 7;

Page 20: 13 주 강의 Structures and Unions. Structures Structure – 기본 자료형들로 구성된 새로운 자료형 예 ) struct card { intpips; char suit; };

p.425 예/*In file numbers.x */typedef union int_or_float { int i; float f;} number;int main(){

number n;n.i = 4444;printf(“i:%10d f:16.10e\n”, n.i, n.f);n.f = 4444.0;printf(“i:%10d f:16.10e\n”, n.i, n.f);return 0;

}

Page 21: 13 주 강의 Structures and Unions. Structures Structure – 기본 자료형들로 구성된 새로운 자료형 예 ) struct card { intpips; char suit; };

Bit Field• struct pcard {

unsigned pips : 4;unsigned suit : 2;

}• bit_field_number::={int|unsigned}1{identif

ier}opt:expr• expr ::=constant_integral_expression

Page 22: 13 주 강의 Structures and Unions. Structures Structure – 기본 자료형들로 구성된 새로운 자료형 예 ) struct card { intpips; char suit; };

Bit Field 의 예• struct abc {

int a : 1, b : 16, c : 16;} x;

• struct small_integers {unsigned i1:7, i2:7, i3:7, :11, /*align to next word*/i4:7, i5:7, i6:7;

}• struct abc {

unsigned a:1, :0, b:1, :0, c:1;};

Page 23: 13 주 강의 Structures and Unions. Structures Structure – 기본 자료형들로 구성된 새로운 자료형 예 ) struct card { intpips; char suit; };

p.429 예/* in file check_bits.x */#include <stdio.h>typedef struct {

unsigned b0:8, b1:8, b2:8, b3:8;} word_bytes;typedef struct {

unsigned b0:1, b1:1, b2:1, b3:1, b4:1, b6:1, b7:1, b8:1, b9:1, b10:1,b11:1,b12:1, b13:1, b14:1, b15:1,

b16:1, b17:1, b18:1, b19:1,b20:1, b21:1,b22:1, b23:1, b24:1, b25:1, b26:1, b27:1, b28:1,b29:1, b30:1,b31:} word_bits;typedef union {

int I;word_bits bit;word_bytes byte;

} word;void main() {

word w = {0};void bit_print(int)lw.bit.b8 = 1;w.byte.b0 = ‘a’;printf(“w.I = %d\n”, w.I);bit_print(w.I);

}

Page 24: 13 주 강의 Structures and Unions. Structures Structure – 기본 자료형들로 구성된 새로운 자료형 예 ) struct card { intpips; char suit; };

ADT Stack

• ADT(Abstract Data Type)• Stack

– last-in-first-out(LIFO)– push,pop,top,empty,full,reset 등

• Stack 의 구현 (p.431)• 문제

– 교재의 Stack 을 사용하여 아래의 식을 계산하라• (13 + 16) * 8 = ???• 단 , 위 식은 키보드에서 “ ( )” 를 포함한 수식을 입력받으며 ,

반드시 “ ( )” 에 대한 Error 처리가 되어야 한다 .– (11 + 3 )) => ERROR!!!

• 사칙연산이 가능해야 한다