1 Lab guide 9 C Structures Unions Bit

1 Lab guide #9 C Structures, Unions, Bit Manipulations and Enumerations Review & Lab Assignments 2007 Pearson Education, Inc. All rights reserved.

2 Key points § Understanding & using – Structures – Unions – Enumerations. § Bitwise operators. 2007 Pearson Education, Inc. All rights reserved.

3 Structure § Structure: – collection of related variables struct card { char *face; char *suit; }; § Definition: 2 ways card one. Card, deck[ 52 ], *c. Ptr; – Or: struct card { char *face; char *suit; } one. Card, deck[ 52 ], *c. Ptr; § Accessing structure members – (. ) used with structure variables – (->) used with pointers to structure variables § typedef: create shorter type names typedef struct Card *Card. Ptr; 2007 Pearson Education, Inc. All rights reserved.

4 Unions § Union: like structure but – Only contains one data member at a time – Members of a union share space – Only the last data member defined can be accessed § union definitions – Same as struct union Number { int x; float y; }; union Number value; 2007 Pearson Education, Inc. All rights reserved.

5 Enumeration § Enumeration – Set of integer constants represented by identifiers – Values are automatically set – Example: enum Months { JAN = 1, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC}; - Creates a new type enum Months in which the identifiers are set to the integers 1 to 12 – Enumeration variables can only assume their enumeration constant values (not the integer representations) 2007 Pearson Education, Inc. All rights reserved.

6 10. 9 Bitwise Operators 2007 Pearson Education, Inc. All rights reserved.

7 Bit Fields § Bit field : Enable better memory utilization struct Bit. Card { unsigned face : 4; unsigned suit : 2; unsigned color : 1; }; – Must be defined as int or unsigned – Cannot access individual bits – Member of a structure whose size (in bits) has been specified 2007 Pearson Education, Inc. All rights reserved.

8 Lab assignments § fig 10_02. c (lecture note) § fig 10_09. c (lecture note) ( Checking if we have time) § Problem 10. 9 (C How to program – Fifth Edition – the question is below) 2007 Pearson Education, Inc. All rights reserved.
- Slides: 8