1 - University of Pittsburgh

1 0 C Structures, Unions, 1 Bit Manipulations and Enumerations 2007 Pearson Education, Inc. All rights reserved. 2 10.1 Introduction 10.2 Structure Definitions 10.3 Initializing Structures 10.4 Accessing Members of Structures 10.5 Using Structures with Functions 10.6 typedef 10.7 Example: High-Performance Card Shuffling and Dealing Simulation

10.8 Unions 10.9 Bitwise Operators 10.10 Bit Fields 10.11 Enumeration Constants 2007 Pearson Education, Inc. All rights reserved. 3 10.1 Introduction Structures Collections of related variables (aggregates) under one name - Can contain variables of different data types Commonly used to define records to be stored in files Combined with pointers, can create linked lists, stacks, queues, and trees 2007 Pearson Education, Inc. All rights reserved. 4 10.2 Structure Definitions Example struct card { char *face; char *suit; }; struct introduces the definition for structure card card is the structure name and is used to declare variables of the structure type

card contains two members of type char * - These members are face and suit 2007 Pearson Education, Inc. All rights reserved. 5 10.2 Structure Definitions struct information A struct cannot contain an instance of itself Can contain a member that is a pointer to the same structure type A structure definition does not reserve space in memory - Instead creates a new data type used to define structure variables Definitions Defined like other variables: card oneCard, deck[ 52 ], *cPtr; Can use a comma separated list: struct card { char *face; char *suit; } oneCard, deck[ 52 ], *cPtr; 2007 Pearson Education, Inc. All rights reserved. 6 10.2 Structure Definitions Valid Operations Assigning a structure to a structure of the same type Taking the address (&) of a structure Accessing the members of a structure Using the sizeof operator to determine the size of a structure 2007 Pearson Education, Inc. All rights reserved. 7

Fig. 10.1 | Possible storage alignment for a variable of type struct example showing an undefined area in memory. 2007 Pearson Education, Inc. All rights reserved. 8 Portability Tip 10.1 Because the size of data items of a particular type is machine dependent and because storage alignment considerations are machine dependent, so too is the representation of a structure. 2007 Pearson Education, Inc. All rights reserved. 9 10.3 Initializing Structures Initializer lists Example: card oneCard = { "Three", "Hearts" }; Assignment statements Example: card threeHearts = oneCard; Could also define and initialize threeHearts as follows: card threeHearts; threeHearts.face = Three; threeHearts.suit = Hearts; 2007 Pearson Education, Inc. All rights reserved. 10 10.4 Accessing Members of Structures Accessing structure members Dot operator (.) used with structure variables card myCard;

printf( "%s", myCard.suit ); Arrow operator (->) used with pointers to structure variables card *myCardPtr = &myCard; printf( "%s", myCardPtr->suit ); myCardPtr->suit is equivalent to ( *myCardPtr ).suit 2007 Pearson Education, Inc. All rights reserved. 1 /* Fig. 10.2: fig10_02.c 2 Using the structure member and 3 structure pointer operators */ 4 Outline 11 #include 5 6 /* card structure definition */ 7 struct card {

Structure definition 8 char *face; /* define pointer face */ 9 char *suit; /* define pointer suit */ fig10_02.c (1 of 2 ) 10 }; /* end structure card */ 11 12 int main( void ) Structure definition must end with semicolon 13 { 14 struct card aCard; /* define one struct card variable */ 15 struct card *cardPtr; /* define a pointer to a struct card */ 16 17 /* place strings into aCard */ 18 aCard.face = "Ace"; 19

aCard.suit = "Spades"; Dot operator accesses members of a structure 2007 Pearson Education, Inc. All rights reserved. 20 21 cardPtr = &aCard; /* assign address of aCard to cardPtr */ 22 23 printf( "%s%s%s\n%s%s%s\n%s%s%s\n", aCard.face, " of ", aCard.suit, 24 cardPtr->face, " of ", cardPtr->suit, 25 ( *cardPtr ).face, " of ", ( *cardPtr ).suit ); 26 27 return 0; /* indicates successful termination */ 28 29 } /* end main */ Outline 12 fig10_02.c (2 of 2 ) Ace of Spades Ace of Spades Ace of Spades Arrow operator accesses members of a structure pointer 2007 Pearson Education,

Inc. All rights reserved. 13 10.5 Using Structures with Functions Passing structures to functions Pass entire structure - Or, pass individual members Both pass call by value Pass a pointer to the structure To pass structures call-by-reference Pass its address Pass reference to it To pass arrays call-by-value Create a structure with the array as a member Pass the structure 2007 Pearson Education, Inc. All rights reserved. 14 Performance Tip 10.1 Passing structures by reference is more efficient than passing structures by value (which requires the entire structure to be copied). 2007 Pearson Education, Inc. All rights reserved. 15 10.6 typedef typedef Creates synonyms (aliases) for previously defined data types Use typedef to create shorter type names Example: typedef struct Card *CardPtr;

Defines a new type name CardPtr as a synonym for type struct Card * typedef does not create a new data type - Only creates an alias 2007 Pearson Education, Inc. All rights reserved. 10.7 Example: High-Performance Card Shuffling and Dealing Simulation 16 Pseudocode: Create an array of card structures Put cards in the deck Shuffle the deck Deal the cards 2007 Pearson Education, Inc. All rights reserved. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

20 21 22 23 24 25 26 27 28 29 /* Fig. 10.3: fig10_03.c The card shuffling and dealing program using structures */ #include #include #include /* card structure definition */ struct card { const char *face; /* define pointer face */ const char *suit; /* define pointer suit */ }; /* end structure card */ Outline 17 fig10_03.c (1 of 3 ) Each card has a face and a suit typedef struct card Card; /* new type name for struct card */ /* prototypes */ void fillDeck( Card * const wDeck, const char * wFace[], const char * wSuit[] ); void shuffle( Card * const wDeck ); void deal( const Card * const wDeck ); Card is now an alias for struct card

int main( void ) { Card deck[ 52 ]; /* define array of Cards */ /* initialize array of pointers */ const char *face[] = { "Ace", "Deuce", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"}; 2007 Pearson Education, Inc. All rights reserved. 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56

/* initialize array of pointers */ const char *suit[] = { "Hearts", "Diamonds", "Clubs", "Spades"}; Outline 18 srand( time( NULL ) ); /* randomize */ fillDeck( deck, face, suit ); /* load the deck with Cards */ shuffle( deck ); /* put Cards in random order */ deal( deck ); /* deal all 52 Cards */ fig10_03.c (2 of 3 ) return 0; /* indicates successful termination */ } /* end main */ /* place strings into Card structures */ void fillDeck( Card * const wDeck, const char * wFace[], const char * wSuit[] ) { Constant pointer to int i; /* counter */ modifiable array of /* loop through wDeck */ for ( i = 0; i <= 51; i++ ) { wDeck[ i ].face = wFace[ i % 13 ]; wDeck[ i ].suit = wSuit[ i / 13 ]; } /* end for */ Cards Fills the deck by giving each Card a face and suit } /* end function fillDeck */ 2007 Pearson Education, Inc. All rights reserved.

57 /* shuffle cards */ 58 void shuffle( Card * const wDeck ) Outline 59 { 60 int i; 61 62 int j; /* variable to hold random value between 0 - 51 */ Card temp; /* define temporary structure for swapping Cards */ 63 64 /* loop through wDeck randomly swapping Cards */ 65 66 for ( i = 0; i <= 51; i++ ) { j = rand() % 52; /* counter */ 67 temp = wDeck[ i ]; 68 69 wDeck[ i ] = wDeck[ j ]; wDeck[ j ] = temp;

70 71 19 fig10_03.c (3 of 3 ) Each card is swapped with another, random card, shuffling the deck } /* end for */ 72 } /* end function shuffle */ 73 74 /* deal cards */ 75 void deal( const Card * const wDeck ) 76 { 77 int i; /* counter */ 78 79 80 81 82 83 84 /* loop through wDeck */ for ( i = 0; i <= 51; i++ ) { printf( "%5s of %-8s%c", wDeck[ i ].face, wDeck[ i ].suit, ( i + 1 ) % 2 ? '\t' : '\n' ); } /* end for */ 85 } /* end function deal */ 2007 Pearson Education, Inc. All rights reserved.

Four of Clubs Three of Diamonds Four of Diamonds Nine of Hearts Three of Clubs Eight of Clubs Deuce of Clubs Seven of Clubs Ace of Clubs Ace of Spades Seven of Diamonds Eight of Spades Five of Spades Queen of Spades Queen of Diamonds Jack of Diamonds Eight of Hearts King of Spades Eight of Diamonds Ace of Hearts Four of Spades Deuce of Hearts Deuce of Spades Seven of Spades King of Clubs Ten of Hearts Three of Hearts Three of Spades Ace of Diamonds Ten of Clubs Four of Hearts Nine of Diamonds Queen of Clubs Jack of Spades Five of Diamonds Five of Clubs Six of Spades Queen of Hearts

Deuce of Diamonds Six of Hearts Seven of Hearts Nine of Spades Five of Hearts Six of Clubs Ten of Spades King of Hearts Jack of Hearts Jack of Clubs Ten of Diamonds Nine of Clubs Six of Diamonds King of Diamonds Outline 20 2007 Pearson Education, Inc. All rights reserved. 21 10.8 Unions union Memory that contains a variety of objects over time Only contains one data member at a time Members of a union share space Conserves storage 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.

22 10.8 Unions Valid union operations Assignment to union of same type: = Taking address: & Accessing union members: . Accessing members using pointers: -> 2007 Pearson Education, Inc. All rights reserved. 23 Software Engineering Observation 10.1 As with a struct definition, a union definition simply creates a new type. Placing a union or struct definition outside any function does not create a global variable. 2007 Pearson Education, Inc. All rights reserved. 24 Portability Tip & Performance10.4 The amount of storage required to store a union is implementation dependent but will always be at least as large as the largest member of the union. Unions conserve storage. 2007 Pearson Education, Inc. All rights reserved. 1 /* Fig. 10.5: fig10_05.c

3 Outline An example of a union */ 2 #include 25 4 5 /* number union definition */ 6 union number { 7 int x; 8 double y; 9 Union definition (1 of 2 ) }; /* end union number */ 10 11 int main( void )

fig10_05.c Union definition must end with semicolon 12 { 13 union number value; /* define union variable */ 14 15 value.x = 100; /* put an integer into the union */ 16 printf( "%s\n%s\n%s\n %d\n\n%s\n Note that y has no value %f\n\n\n", 17 "Put a value in the integer member", 18 "and print both members.", 19 "int:", value.x, 20 "double:", value.y );

21 2007 Pearson Education, Inc. All rights reserved. 22 value.y = 100.0; /* put a double into the same union */ 23 printf( "%s\n%s\n%s\n %d\n\n%s\n %f\n", 24 "Put a value in the floating member", 25 "and print both members.", Giving y a 26 "int:", value.x, 27 "double:", value.y ); 28 29 return 0; /* indicates successful termination */ 30 31 } /* end main */ Outline 26 value removes xs value fig10_05.c (2 of 2 ) Put a value in the integer member and print both members. int: 100 double: -92559592117433136000000000000000000000000000000000000000000000.000000

Put a value in the floating member and print both members. int: 0 double: 100.000000 2007 Pearson Education, Inc. All rights reserved. 27 10.9 Bitwise Operators All data is represented internally as sequences of bits Each bit can be either 0 or 1 Sequence of 8 bits forms a byte Bitwise data manipulations are machine dependent. 2007 Pearson Education, Inc. All rights reserved. 28 Operator & Description bitwise AND The bits in the result are set to 1 if the corresponding bits in the two operands are both 1 . | bitwise inclusive OR

The bits in the result are set to 1 if at least one of the corresponding bits in the two operands is 1. ^ bitwise exclusive OR The bits in the result are set to 1 if exactly one of the corresponding bits in the two operands is 1. << left shift Shifts the bits of the first operand left by the number of bits specified by the second operand; fill from the right with 0 bits. >> right shift Shifts the bits of the first operand right by the number of bits specified by the second operand; the method of filling from the left is machine dependent. ~ ones complement All 0 bits are set to 1 and all 1 bits are set to 0. Fig. 10.6 | Bitwise operators. 2007 Pearson Education, Inc. All rights reserved. 1 /* Fig. 10.7: fig10_07.c 2 Printing an unsigned integer in bits */ Outline

29 3 #include 4 fig10_07.c 5 void displayBits( unsigned value ); /* prototype */ 6 (1 of 2 ) 7 int main( void ) 8 { 9 unsigned x; /* variable to hold user input */ 10 11 printf( "Enter an unsigned integer: " ); 12 scanf( "%u", &x ); 13 14 displayBits( x ); 15 16 return 0; /* indicates successful termination */ 17 18 } /* end main */ 19

2007 Pearson Education, Inc. All rights reserved. 20 /* display bits of an unsigned integer value */ 21 void displayBits( unsigned value ) Outline 22 { 23 unsigned c; /* counter */ 24 25 /* define displayMask and left shift 31 bits */ 26 27 unsigned displayMask = 1 << 31; 28 29 printf( "%10u = ", value ); 30 /* loop through bits */ 31 32 33 for ( c = 1; c <= 32; c++ ) { putchar( value & displayMask ? '1' : '0' ); value <<= 1; /* shift value left by 1 */

30 fig10_07.c displayMask is a 1 followed by 31 zeros (2 of 2 ) Bitwise AND returns nonzero if the leftmost bits of displayMask and value are both 1, since all other bits in displayMask are 0s. 34 35 36 if ( c % 8 == 0 ) { /* output space after 8 bits */ putchar( ' ' ); 37 38 39 40 } /* end if */ } /* end for */ 41 putchar( '\n' ); 42 } /* end function displayBits */ Enter an unsigned integer: 65000 65000 = 00000000 00000000 11111101 11101000 2007 Pearson Education, Inc. All rights reserved. 31 Common Programming Error 10.11 Using the logical AND operator (&&) for the

bitwise AND operator (&) and vice versa is an error. 2007 Pearson Education, Inc. All rights reserved. 32 Bit 1 Bit 2 Bit 1 & Bit 2 0 0 0 1 0 0 0 1 0 1 1 1 Fig. 10.8 | Results of combining two bits with the bitwise AND operator &. 2007 Pearson Education, Inc. All rights reserved.

33 Bit 1 Bit 2 Bit 1 | Bit 2 0 0 0 1 0 1 0 1 1 1 1 1 Fig. 10.11 | Results of combining two bits with the bitwise inclusive OR operator |. 2007 Pearson Education, Inc. All rights reserved. 34 Bit 1

Bit 2 Bit 1 ^ Bit 2 0 0 0 1 0 1 0 1 1 1 1 0 Fig. 10.12 | Results of combining two bits with the bitwise exclusive OR operator ^. 2007 Pearson Education, Inc. All rights reserved. 1 2 3 4 5 6 7

8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 /* Fig. 10.9: fig10_09.c Using the bitwise AND, bitwise inclusive OR, bitwise exclusive OR and bitwise complement operators */ #include void displayBits( unsigned value ); /* prototype */ int main( void ) { unsigned number1; unsigned number2; unsigned mask; unsigned setBits; Outline 35 fig10_09.c (1 of 3 ) /* demonstrate bitwise AND (&) */ number1 = 65535; mask = 1;

printf( "The result of combining the following\n" ); displayBits( number1 ); displayBits( mask ); printf( "using the bitwise AND operator & is\n" ); displayBits( number1 & mask ); Bitwise AND sets each bit in the result to 1 if the corresponding bits in the operands are both 1 2007 Pearson Education, Inc. All rights reserved. 24 /* demonstrate bitwise inclusive OR (|) */ 36 25 number1 = 15; 26 setBits = 241; 27 printf( "\nThe result of combining the following\n" ); 28 displayBits( number1 ); 29 displayBits( setBits ); fig10_09.c 30 printf( "using the bitwise inclusive OR operator | is\n" ); 31 displayBits( number1 | setBits ); (2 of 3 ) 32 33 /* demonstrate bitwise exclusive OR (^) */ Bitwise inclusive OR sets each bit in the result to 1 if at 34 number1 = 139; least one of the corresponding bits in the operands is 1 35 number2 = 199; 36

printf( "\nThe result of combining the following\n" ); 37 displayBits( number1 ); 38 displayBits( number2 ); 39 printf( "using the bitwise exclusive OR operator ^ is\n" ); 40 displayBits( number1 ^ number2 ); 41 Bitwise exclusive OR sets each bit in the result to 1 if 42 /* demonstrate bitwise complement (~)*/ only one of the corresponding bits in the operands is 1 43 number1 = 21845; 44 printf( "\nThe one's complement of\n" ); 45 displayBits( number1 ); 46 printf( "is\n" ); Complement operator sets each bit in the result to 0 if the 47 displayBits( ~number1 ); corresponding bit in the operand is 1 and vice versa 48 49 return 0; /* indicates successful termination */ 50 } /* end main */ 51 Outline 2007 Pearson Education, Inc. All rights reserved. 52 /* display bits of an unsigned integer value */ 53 void displayBits( unsigned value )

Outline 37 54 { 55 unsigned c; /* counter */ fig10_09.c 56 57 /* declare displayMask and left shift 31 bits */ 58 unsigned displayMask = 1 << 31; (3 of 3 ) 59 60 printf( "%10u = ", value ); 61 62 /* loop through bits */ 63 for ( c = 1; c <= 32; c++ ) { 64 putchar( value & displayMask ? '1' : '0' ); 65

value <<= 1; /* shift value left by 1 */ 66 67 68 69 if ( c % 8 == 0 ) { /* output a space after 8 bits */ putchar( ' ' ); } /* end if */ 70 71 } /* end for */ 72 73 putchar( '\n' ); 74 } /* end function displayBits */ 2007 Pearson Education, Inc. All rights reserved. The result of combining the following 65535 = 00000000 00000000 11111111 11111111 1 = 00000000 00000000 00000000 00000001 using the bitwise AND operator & is 1 = 00000000 00000000 00000000 00000001 The result of combining the following 15 = 00000000 00000000 00000000 241 = 00000000 00000000 00000000 using the bitwise inclusive OR operator 255 = 00000000 00000000 00000000 00001111 11110001

| is 11111111 The result of combining the following 139 = 00000000 00000000 00000000 199 = 00000000 00000000 00000000 using the bitwise exclusive OR operator 76 = 00000000 00000000 00000000 10001011 11000111 ^ is 01001100 Outline 38 fig10_10.c The one's complement of 21845 = 00000000 00000000 01010101 01010101 is 4294945450 = 11111111 11111111 10101010 10101010 2007 Pearson Education, Inc. All rights reserved. 39 Common Programming Error 10.12 Using the logical OR operator (||) for the bitwise OR operator (|) and vice versa is an error. 2007 Pearson Education, Inc. All rights reserved. 1 2 /* Fig. 10.13: fig10_13.c

Using the bitwise shift operators */ 3 4 #include 5 void displayBits( unsigned value ); /* prototype */ 6 7 8 int main( void ) { Outline 40 fig10_13.c 9 unsigned number1 = 960; /* initialize number1 */ 10 11 /* demonstrate bitwise left shift */ 12 13 printf( "\nThe result of left shifting\n" ); displayBits( number1 ); 14

15 printf( "8 bit positions using the " ); printf( "left shift operator << is\n" ); 16 displayBits( number1 << 8 ); (1 of 3 ) 17 Left shift operator shifts all bits left a specified number of spaces, filling in zeros for the empty bits 2007 Pearson Education, Inc. All rights reserved. 18 /* demonstrate bitwise right shift */ 19 printf( "\nThe result of right shifting\n" ); 20 displayBits( number1 ); 21 printf( "8 bit positions using the " ); 22 23 printf( "right shift operator >> is\n" ); displayBits( number1 >> 8 );

24 25 return 0; /* indicates successful termination */ 26 } /* end main */ 27 28 /* display bits of an unsigned integer value */ 29 void displayBits( unsigned value ) 30 { 30 { 31 32 unsigned c; /* counter */ 33 /* declare displayMask and left shift 31 bits */ 34 35 36 unsigned displayMask = 1 << 31; Outline 41 fig10_13.c (2 of 3 ) Right shift operator shifts all bits right a specified number of spaces, filling in the empty bits in an implementation-defined manner printf( "%7u = ", value );

37 2007 Pearson Education, Inc. All rights reserved. 38 /* loop through bits */ 39 40 for ( c = 1; c <= 32; c++ ) { putchar( value & displayMask ? '1' : '0' ); 41 value <<= 1; /* shift value left by 1 */ Outline 42 fig10_13.c 42 43 if ( c % 8 == 0 ) { /* output a space after 8 bits */ 44 45 putchar( ' ' ); } /* end if */ (3 of 3 ) 46 47

} /* end for */ 48 49 putchar( '\n' ); 50 } /* end function displayBits */ The result of left shifting 960 = 00000000 00000000 00000011 11000000 8 bit positions using the left shift operator << is 245760 = 00000000 00000011 11000000 00000000 The result of right shifting 960 = 00000000 00000000 00000011 11000000 8 bit positions using the right shift operator >> is 3 = 00000000 00000000 00000000 00000011 2007 Pearson Education, Inc. All rights reserved. 43 Common Programming Error 10.13 The result of shifting a value is undefined if the right operand is negative or if the right operand is larger than the number of bits in which the left operand is stored. 2007 Pearson Education, Inc. All rights reserved. 44 Portability Tip 10.7 Right shifting is machine dependent. Right shifting a signed integer fills the vacated bits with 0s on some machines and with 1s on others. 2007 Pearson Education, Inc. All rights reserved. 45

Bitwise assignment operators &= Bitwise AND assignment operator. |= Bitwise inclusive OR assignment operator. ^= Bitwise exclusive OR assignment operator. <<= Left-shift assignment operator. >>= Right-shift assignment operator. Fig. 10.14 | The bitwise assignment operators. 2007 Pearson Education, Inc. All rights reserved. 46 Operators () [ ] . -> ~ sizeof (type) right to left

unary left to right multiplicative / + - left to right additive << >> left to right shiting < <= left to right relational == != left to right equality

left to right bitwise AND >= * highest * > & left to right -- % ! Type + & ++++ -- Associativity Fig. 10.15 | Operator precedence and associativity. (Part 1 of 2.) 2007 Pearson Education, Inc. All rights reserved. 47

Operators Associativity Type ^ left to right bitwise OR | left to right bitwise OR && left to right logical AND || left to right logical OR ?: right to left conditional right to left assignment

left to right comma = , += -= *= /= &= |= ^= <<= >>= %= Fig. 10.15 | Operator precedence and associativity. (Part 2 of 2.) 2007 Pearson Education, Inc. All rights reserved. 48 10.10 Bit Fields Bit field Member of a structure whose size (in bits) has been specified

Enable better memory utilization Must be defined as int or unsigned Cannot access individual bits Defining bit fields Follow unsigned or int member with a colon (:) and an integer constant representing the width of the field Example: struct BitCard { unsigned face : 4; unsigned suit : 2; unsigned color : 1; }; 2007 Pearson Education, Inc. All rights reserved. 49 10.10 Bit Fields Unnamed bit field Field used as padding in the structure Nothing may be stored in the bits struct Example { unsigned a : 13; unsigned : 3; unsigned b : 4; } Unnamed bit field with zero width aligns next bit field to a new storage unit boundary 2007 Pearson Education, Inc. All rights reserved. 50 Performance Tip 10.3 Bit fields help conserve storage.

2007 Pearson Education, Inc. All rights reserved. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 /* Fig. 10.16: fig10_16.c Representing cards with bit fields in a struct */ Outline 51 #include

/* bitCard structure definition with bit fields */ struct bitCard { unsigned face : 4; /* 4 bits; 0-15 */ unsigned suit : 2; /* 2 bits; 0-3 */ unsigned color : 1; /* 1 bit; 0-1 */ Bit fields }; /* end struct bitCard */ fig10_16.c (1 of 2 ) determine how much memory each member of a structure can take up typedef struct bitCard Card; /* new type name for struct bitCard */ void fillDeck( Card * const wDeck ); /* prototype */ void deal( const Card * const wDeck ); /* prototype */ int main( void ) { Card deck[ 52 ]; /* create array of Cards */ fillDeck( deck ); deal( deck ); return 0; /* indicates successful termination */ } /* end main */ 2007 Pearson Education, Inc. All rights reserved. 29 30 31 32 33 34 35 36 37 38

39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 /* initialize Cards */ void fillDeck( Card * const wDeck ) { int i; /* counter */ /* loop through wDeck */ for ( i = 0; i <= 51; i++ ) { wDeck[ i ].face = i % 13; wDeck[ i ].suit = i / 13; wDeck[ i ].color = i / 26; } /* end for */ Outline 52 fig10_16.c (2 of 2 )

} /* end function fillDeck */ /* output cards in two column format; cards 0-25 subscripted with k1 (column 1); cards 26-51 subscripted k2 (column 2) */ void deal( const Card * const wDeck ) { int k1; /* subscripts 0-25 */ int k2; /* subscripts 26-51 */ /* loop through wDeck */ for ( k1 = 0, k2 = k1 + 26; k1 <= 25; k1++, k2++ ) { printf( "Card:%3d Suit:%2d Color:%2d ", wDeck[ k1 ].face, wDeck[ k1 ].suit, wDeck[ k1 ].color ); printf( "Card:%3d Suit:%2d Color:%2d\n", wDeck[ k2 ].face, wDeck[ k2 ].suit, wDeck[ k2 ].color ); } /* end for */ } /* end function deal */ 2007 Pearson Education, Inc. All rights reserved. Card: Card: Card: Card: Card: Card: Card: Card: Card: Card: Card: Card: Card: Card: Card: Card: Card: Card: Card: Card:

Card: Card: Card: Card: Card: Card: 0 1 2 3 4 5 6 7 8 9 10 11 12 0 1 2 3 4 5 6 7 8 9 10 11 12 Suit: Suit: Suit: Suit: Suit: Suit:

Suit: Suit: Suit: Suit: Suit: Suit: Suit: Suit: Suit: Suit: Suit: Suit: Suit: Suit: Suit: Suit: Suit: Suit: Suit: Suit: 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1

1 1 1 1 1 1 1 Color: Color: Color: Color: Color: Color: Color: Color: Color: Color: Color: Color: Color: Color: Color: Color: Color: Color: Color: Color: Color: Color: Color: Color: Color: Color: 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Card: Card: Card: Card: Card: Card: Card: Card: Card: Card: Card: Card: Card: Card: Card: Card: Card: Card:

Card: Card: Card: Card: Card: Card: Card: Card: 0 1 2 3 4 5 6 7 8 9 10 11 12 0 1 2 3 4 5 6 7 8 9 10 11 12 Suit: Suit: Suit: Suit:

Suit: Suit: Suit: Suit: Suit: Suit: Suit: Suit: Suit: Suit: Suit: Suit: Suit: Suit: Suit: Suit: Suit: Suit: Suit: Suit: Suit: Suit: 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3

3 3 3 3 3 3 3 3 3 Color: Color: Color: Color: Color: Color: Color: Color: Color: Color: Color: Color: Color: Color: Color: Color: Color: Color: Color: Color: Color: Color: Color: Color: Color: Color: 1 1 1

1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Outline 53 2007 Pearson Education, Inc. All rights reserved. 54 Portability Tip 10.8 Bit-field manipulations are machine dependent. For example, some computers allow bit fields to cross word boundaries, whereas others do not. 2007 Pearson Education, Inc. All rights reserved.

55 Common Programming Error 10.14 Attempting to access individual bits of a bit field as if they were elements of an array is a syntax error. Bit fields are not arrays of bits. 2007 Pearson Education, Inc. All rights reserved. 56 Common Programming Error 10.15 Attempting to take the address of a bit field (the & operator may not be used with bit fields because they do not have addresses). 2007 Pearson Education, Inc. All rights reserved. 57 Performance Tip 10.4 Although bit fields save space, using them can cause the compiler to generate slower-executing machine-language code. This occurs because it takes extra machine language operations to access only portions of an addressable storage unit. This is one of many examples of the kinds of spacetime trade-offs that occur in computer science. 2007 Pearson Education, Inc. All rights reserved. 58 10.11 Enumeration Constants Enumeration Set of integer constants represented by identifiers Enumeration constants are like symbolic constants whose values are automatically set - Values start at 0 and are incremented by 1

- Values can be set explicitly with = - Need unique constant names 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. 1 /* Fig. 10.18: fig10_18.c Using an enumeration type */ 2 3 #include Outline 59 4 5 /* enumeration constants represent months of the year */ 6 enum months { JAN = 1, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC }; 7

8 9 int main( void ) 10 { 11 Enumeration sets the value of constant JAN to 1 and the following constants to 2, 3, 4 fig10_18.c (1 of 2 ) enum months month; /* can contain any of the 12 months */ 12 13 /* initialize array of pointers */ 14 const char *monthName[] = { "", "January", "February", "March", 15 "April", "May", "June", "July", "August", "September", "October", 16 "November", "December" }; 17 2007 Pearson Education, Inc. All rights reserved. 18

/* loop through months */ 19 for ( month = JAN; month <= DEC; month++ ) { 20 printf( "%2d%11s\n", month, monthName[ month ] ); 21 } /* end for */ 22 23 return 0; /* indicates successful termination */ 24 } /* end main */ 1 2 3 4 5 6 7 8 9 10 11 12 January February March April May June July August September October November December Outline 60

fig10_18.c (2 of 2 ) Like symbolic constants, enumeration constants are replaced by their values at compile time 2007 Pearson Education, Inc. All rights reserved. 61 Common Programming Error 10.16 Assigning a value to an enumeration constant after it has been defined is a syntax error. 2007 Pearson Education, Inc. All rights reserved. 62 Good Programming Practice 10.5 Use only uppercase letters in the names of enumeration constants. This makes these constants stand out in a program and reminds you that enumeration constants are not variables. 2007 Pearson Education, Inc. All rights reserved.

Recently Viewed Presentations

  • The Four C&#x27;s of Good Writing

    The Four C's of Good Writing

    The Four C's of Good Writing Brenham Writing Room Created by D. Herring What are the Four C's? Good Writing Is . . . Concise Credible Clear Correct What is Concise Writing? Concise writing has a clear point and includes...
  • Estate Planning: Caring For Your Loved Ones Florida

    Estate Planning: Caring For Your Loved Ones Florida

    Florida Retirement System. Successful retirement planning includes planning for all aspects of one's . financial life. Program is sponsored by Florida Retirement System, is available at no cost to you, and can offer you guidance on a wide range of...
  • Voiding Dysfunction &amp; BPH

    Voiding Dysfunction & BPH

    Video-urodynamics. Endoscopy (Cystoscopy) Upper tract imaging (renal ultrasound) Voiding. ... Before and After TURP. BEFORE. AFTER. This slide shows the prostate gland and the urethra, before and after surgery. Urine flows easier once the obstruction has been removed.
  • Le Subjonctif Le placement des pronoms 1. Emily

    Le Subjonctif Le placement des pronoms 1. Emily

    1. Emily prend des photos à Port Moody. 2. Erin et Alexis dessinent des dessins. 3. Hajer a demandé la question à toi. 4. Parsa chante les chansons en classe.
  • David F. Miller Center For Retailing Education and

    David F. Miller Center For Retailing Education and

    * Picture: Lao Tzu, the founder of Daosim Taoism (also spelled Daoism) refers to a philosophical or religious tradition in which the basic concept is to establish harmony with the Tao (道), which is the mechanism of everything that exists.
  • WHAT CAUSES CLIMATE - monroe.k12.ky.us

    WHAT CAUSES CLIMATE - monroe.k12.ky.us

    WHAT CAUSES CLIMATE. ... Two main factors of a climate are temperature and precipitation. FACTORS AFFECTING TEMPERATURE. Latitude: Based on latitude, Earth's surface can be divided into three temperature zones; Tropical, temperate, and polar. ... Windward is the side of...
  • A draft of Interaction Region with Crab Waist

    A draft of Interaction Region with Crab Waist

    Low emittance FODO cell DA (I) Emittance decreases with focusing increase but for too strong focusing emittance grows up again. DA generally decreases with focusing increase. Strong sextupole resonances form gaps in the DA profile. Ax>80 Ax>60 x = 80...
  • Vultures Chinua Achebe - Mrs Ruxton

    Vultures Chinua Achebe - Mrs Ruxton

    As you watch the performance, think carefully about the impression we are given of the Duke and try to work out what you think happened to his Last Duchess. Once you have watched the clip and read the poem, you...