1 / 8

Lab guide #9

Lab guide #9. C Structures, Unions, Bit Manipulations and Enumerations Review & Lab Assignments. Key points. Understanding & using Structures Unions Enumerations. Bitwise operators. Structure. Structure: collection of related variables struct card { char *face;

majed
Télécharger la présentation

Lab guide #9

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lab guide #9 C Structures, Unions, Bit Manipulations and Enumerations Review & Lab Assignments

  2. Key points • Understanding & using • Structures • Unions • Enumerations. • Bitwise operators.

  3. Structure • Structure: • collection of related variables struct card { char *face; char *suit; }; • Definition: 2 ways card oneCard, deck[ 52 ], *cPtr; • Or: struct card { char *face; char *suit; } oneCard, deck[ 52 ], *cPtr; • Accessing structure members • (.) used with structure variables • (->) used with pointers to structure variables • typedef: create shorter type names typedefstruct Card *CardPtr;

  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;

  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)

  6. 10.9 Bitwise Operators

  7. Bit Fields • Bit field : Enable better memory utilization struct BitCard { 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

  8. fig10_02.c (lecture note) fig10_09.c (lecture note) ( Checking if we have time) Problem 10.9 (C How to program – Fifth Edition – the question is below) Lab assignments

More Related