13 Structures and Unions Structures Structure struct card





![예 • struct card { int pips; char suit; } deck[52]; • struct date 예 • struct card { int pips; char suit; } deck[52]; • struct date](https://slidetodoc.com/presentation_image_h2/8ece5d26d33dd8680652e0f13027cd68/image-6.jpg)





![Operator Precedence and Associativity Operator () [] ++(prefix) +(unary) . -> --(prefix) -(unary) ++(postfix) Operator Precedence and Associativity Operator () [] ++(prefix) +(unary) . -> --(prefix) -(unary) ++(postfix)](https://slidetodoc.com/presentation_image_h2/8ece5d26d33dd8680652e0f13027cd68/image-12.jpg)
![Using structures with functions • typedef struct { char name[25]; int employee_id; struct deparment; Using structures with functions • typedef struct { char name[25]; int employee_id; struct deparment;](https://slidetodoc.com/presentation_image_h2/8ece5d26d33dd8680652e0f13027cd68/image-13.jpg)
![Cont. • struct dept { char dept_name[25]; int dept_no; }; • e. department. dept_no Cont. • struct dept { char dept_name[25]; int dept_no; }; • e. department. dept_no](https://slidetodoc.com/presentation_image_h2/8ece5d26d33dd8680652e0f13027cd68/image-14.jpg)

![Cont. • complex[3][3] = { {{1. 0, -0. 1}, {2. 0, 0. 2}, {3. Cont. • complex[3][3] = { {{1. 0, -0. 1}, {2. 0, 0. 2}, {3.](https://slidetodoc.com/presentation_image_h2/8ece5d26d33dd8680652e0f13027cd68/image-16.jpg)








- Slides: 24

13주 강의 Structures and Unions

Structures • Structure – 기본 자료형들로 구성된 새로운 자료형 • 예) struct card { int pips; char suit; };

Structures struct card { int pips; char suit; }; struct card { int pips; char suit; } c 1, c 2; ※ pips, suit를 멤버로 가지는 card Type의 c 1, c 2 선언

값 저장 • • • c 1. pips = 3; c 1. suit = ‘s’; c 2=c 1; /* structure 전체를 복사 */ typedef struct card; card c 3, c 4, c 5;

다른 예 • struct fruit { char *name; int calories; } • struct vegetable { char *name; int calories; } • struct fruit a; • struct vegetable b;
![예 struct card int pips char suit deck52 struct date 예 • struct card { int pips; char suit; } deck[52]; • struct date](https://slidetodoc.com/presentation_image_h2/8ece5d26d33dd8680652e0f13027cd68/image-6.jpg)
예 • 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, tomorrow;

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

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 412 프로그램 설명 • -> – pointer_to_structure->member_name (*pointer_to_structure). member_name /* ()가 없으면 오류 */ • complex *a, *b, *c; – a->re = b-> re + c -> re;

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

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
![Operator Precedence and Associativity Operator prefix unary prefix unary postfix Operator Precedence and Associativity Operator () [] ++(prefix) +(unary) . -> --(prefix) -(unary) ++(postfix)](https://slidetodoc.com/presentation_image_h2/8ece5d26d33dd8680652e0f13027cd68/image-12.jpg)
Operator Precedence and Associativity Operator () [] ++(prefix) +(unary) . -> --(prefix) -(unary) ++(postfix) ! ~ &(address) % --(postfix) sizeof(type) *(dereference) Associativity left to 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 ? : right to left = = += -= / %= >>= <<= &= , (comma operator) right to left ^= |= left to right
![Using structures with functions typedef struct char name25 int employeeid struct deparment Using structures with functions • typedef struct { char name[25]; int employee_id; struct deparment;](https://slidetodoc.com/presentation_image_h2/8ece5d26d33dd8680652e0f13027cd68/image-13.jpg)
Using structures with functions • typedef struct { char name[25]; int employee_id; struct deparment; struct home_address *a_ptr; double salary; …. } employee_data;
![Cont struct dept char deptname25 int deptno e department deptno Cont. • struct dept { char dept_name[25]; int dept_no; }; • e. department. dept_no](https://slidetodoc.com/presentation_image_h2/8ece5d26d33dd8680652e0f13027cd68/image-14.jpg)
Cont. • struct dept { char dept_name[25]; int dept_no; }; • e. department. dept_no (e. department). dept_no • employee_data e; e = update(e);

Initialization of Structures • card c={13, ’h’}; • struct fruit frt={“plum”, 150); • struct home_address { char long *street; *city_and_state; zip_code; } address = {“ 87 West Street”, ”Aspen, Colorado”, 80526}; • struct home_address = {0}; previous_address
![Cont complex33 1 0 0 1 2 0 0 2 3 Cont. • complex[3][3] = { {{1. 0, -0. 1}, {2. 0, 0. 2}, {3.](https://slidetodoc.com/presentation_image_h2/8ece5d26d33dd8680652e0f13027cd68/image-16.jpg)
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 설명


예 • union int_or_float n; n. i = 4444; n. f = 444. 0; n. i 4444 n. f 0. 6227370375 e-41 n. f 4444. 0 n. i 116672921

다른 예 • 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. veg. cooking_time = 7; ffv;

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: %10 d f: 16. 10 en”, n. i, n. f); n. f = 4444. 0; printf(“i: %10 d f: 16. 10 en”, n. i, n. f); return 0; }

Bit Field • struct pcard { unsigned pips : 4; unsigned suit : 2; } • bit_field_number: : ={int|unsigned}1{id entifier}opt: expr • expr : : =constant_integral_expression

Bit Field의 예 • struct abc { int a : 1, b : 16, c : 16; } x; • struct small_integers { unsigned i 1: 7, i 2: 7, i 3: 7, : 11, /*align to next word*/ i 4: 7, i 5: 7, i 6: 7; } • struct abc { unsigned a: 1, : 0, b: 1, : 0, c: 1; };

p. 429 예 /* in file check_bits. x */ #include <stdio. h> typedef struct { unsigned b 0: 8, b 1: 8, b 2: 8, b 3: 8; } word_bytes; typedef struct { unsigned b 0: 1, b 1: 1, b 2: 1, b 3: 1, b 4: 1, b 6: 1, b 7: 1, b 8: 1, b 9: 1, b 10: 1, b 11: 1, b 12: 1, b 13: 1, b 14: 1, b 15: 1, b 16: 1, b 17: 1, b 18: 1, b 19: 1, b 20: 1, b 21: 1, b 22: 1, b 23: 1, b 24: 1, b 25: 1, b 26: 1, b 27: 1, b 28: 1, b 29: 1, b 30: 1, b 31: } word_bits; typedef union { int I; word_bits bit; word_bytes byte; } word; void main() { word w = {0}; void bit_print(int)l w. bit. b 8 = 1; w. byte. b 0 = ‘a’; printf(“w. I = %dn”, w. I); bit_print(w. I); }
