10
C Structures, Unions, Bit Manipulations and Enumerations
1
© 2007 Pearson Education, Inc. All rights reserved.
But yet an union in partition.
The same old charitable lie�Repeated as the years scoot by�Perpetually makes a hit—
“You really haven’t changed a bit!”
I could never make out what those�damned dots meant.
2
© 2007 Pearson Education, Inc. All rights reserved.
OBJECTIVES
In this chapter you will learn:
3
© 2007 Pearson Education, Inc. All rights reserved.
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
4
© 2007 Pearson Education, Inc. All rights reserved.
10.1 Introduction
5
© 2007 Pearson Education, Inc. All rights reserved.
10.2 Structure Definitions
struct card {
char *face;
char *suit;� };
6
© 2007 Pearson Education, Inc. All rights reserved.
Common Programming Error 10.1
Forgetting the semicolon that terminates�a structure definition is a syntax error.
7
© 2007 Pearson Education, Inc. All rights reserved.
10.2 Structure Definitions
card oneCard, deck[ 52 ], *cPtr;
struct card {
char *face;
char *suit;
} oneCard, deck[ 52 ], *cPtr;
8
© 2007 Pearson Education, Inc. All rights reserved.
Good Programming Practice 10.1
Always provide a structure tag name when creating a structure type. The structure tag name is convenient for declaring new variables of the structure type later in the program.
9
© 2007 Pearson Education, Inc. All rights reserved.
Good Programming Practice 10.2
Choosing a meaningful structure tag name helps make a program self-documenting.
10
© 2007 Pearson Education, Inc. All rights reserved.
10.2 Structure Definitions
11
© 2007 Pearson Education, Inc. All rights reserved.
Common Programming Error 10.2
Assigning a structure of one type to a structure of a different type is a compilation error.
12
© 2007 Pearson Education, Inc. All rights reserved.
Common Programming Error 10.3
Comparing structures is a syntax error.
13
© 2007 Pearson Education, Inc. All rights reserved.
14
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.
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.
15
© 2007 Pearson Education, Inc. All rights reserved.
10.3 Initializing Structures
card oneCard = { "Three", "Hearts" };
card threeHearts = oneCard;
card threeHearts;
threeHearts.face = “Three”;
threeHearts.suit = “Hearts”;
16
© 2007 Pearson Education, Inc. All rights reserved.
10.4 Accessing Members of Structures
card myCard;
printf( "%s", myCard.suit );
card *myCardPtr = &myCard;
printf( "%s", myCardPtr->suit );
( *myCardPtr ).suit
17
© 2007 Pearson Education, Inc. All rights reserved.
Error-Prevention Tip 10.1
Avoid using the same names for members of structures of different types. This is allowed, but it may cause confusion.
18
© 2007 Pearson Education, Inc. All rights reserved.
Good Programming Practice 10.3
Do not put spaces around the -> and .�operators. Omitting spaces helps emphasize that the expressions the operators are contained in are essentially single variable names.
19
© 2007 Pearson Education, Inc. All rights reserved.
Common Programming Error 10.4
Inserting space between the - and > components of the structure pointer operator (or between the components of any other multiple keystroke operator except ?:) is a syntax error.
20
© 2007 Pearson Education, Inc. All rights reserved.
Common Programming Error 10.5
Attempting to refer to a member of a structure by using only the member’s name is a syntax error.
21
© 2007 Pearson Education, Inc. All rights reserved.
Common Programming Error 10.6
Not using parentheses when referring to�a structure member that uses a pointer and the structure member operator�(e.g., *cardPtr.suit) is a syntax error.
22
© 2007 Pearson Education, Inc. All rights reserved.
23
Outline
fig10_02.c
(1 of 2 )
Structure definition
Structure definition must end with semicolon
Dot operator accesses members of a structure
© 2007 Pearson Education, Inc. All rights reserved.
24
Outline
fig10_02.c
(2 of 2 )
Arrow operator accesses members of a structure pointer
© 2007 Pearson Education, Inc. All rights reserved.
10.5 Using Structures with Functions
25
© 2007 Pearson Education, Inc. All rights reserved.
Common Programming Error 10.7
Assuming that structures, like arrays, are automatically passed by reference and trying to modify the caller’s structure values in the called function is a logic error.
26
© 2007 Pearson Education, Inc. All rights reserved.
Performance Tip 10.1
Passing structures by reference is more efficient than passing structures by value (which requires the entire structure to be copied).
27
© 2007 Pearson Education, Inc. All rights reserved.
10.6 typedef
typedef struct Card *CardPtr;
28
© 2007 Pearson Education, Inc. All rights reserved.
Good Programming Practice 10.4
Capitalize the first letter of typedef names to emphasize that they are synonyms for other type names.
29
© 2007 Pearson Education, Inc. All rights reserved.
Portability Tip 10.2
Use typedef to help make a program�more portable.
30
© 2007 Pearson Education, Inc. All rights reserved.
10.7 Example: High-Performance Card Shuffling and Dealing Simulation
31
© 2007 Pearson Education, Inc. All rights reserved.
32
Outline
fig10_03.c
(1 of 3 )
Each card has a face and a suit
Card is now an alias for struct card
© 2007 Pearson Education, Inc. All rights reserved.
33
Outline
fig10_03.c
(2 of 3 )
Constant pointer to modifiable array of Cards
Fills the deck by giving each Card a face and suit
© 2007 Pearson Education, Inc. All rights reserved.
34
Outline
fig10_03.c
(3 of 3 )
Each card is swapped with another, random card, shuffling the deck
© 2007 Pearson Education, Inc. All rights reserved.
35
Outline
© 2007 Pearson Education, Inc. All rights reserved.
Common Programming Error 10.8
Forgetting to include the array subscript when referring to individual structures in an array of structures is a syntax error.
36
© 2007 Pearson Education, Inc. All rights reserved.
10.8 Unions
union Number {
int x;
float y;
};
union Number value;
37
© 2007 Pearson Education, Inc. All rights reserved.
10.8 Unions
38
© 2007 Pearson Education, Inc. All rights reserved.
Common Programming Error 10.9
Referencing data in a union with a�variable of the wrong type is a logic error.
39
© 2007 Pearson Education, Inc. All rights reserved.
Portability Tip 10.3
If data is stored in a union as one type and referenced as another type, the results are implementation dependent.
40
© 2007 Pearson Education, Inc. All rights reserved.
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.
41
© 2007 Pearson Education, Inc. All rights reserved.
Common Programming Error 10.10
Comparing unions is a syntax error.
42
© 2007 Pearson Education, Inc. All rights reserved.
Portability Tip 10.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.
43
© 2007 Pearson Education, Inc. All rights reserved.
Portability Tip 10.5
Some unions may not port easily to other computer systems. Whether a union is portable or not often depends on the storage alignment requirements for the union member data types on a given system.
44
© 2007 Pearson Education, Inc. All rights reserved.
Performance Tip 10.2
Unions conserve storage.
45
© 2007 Pearson Education, Inc. All rights reserved.
46
Outline
fig10_05.c
(1 of 2 )
Union definition
Union definition must end with semicolon
Note that y has no value
© 2007 Pearson Education, Inc. All rights reserved.
47
Outline
fig10_05.c
(2 of 2 )
Giving y a value removes x’s value
© 2007 Pearson Education, Inc. All rights reserved.
10.9 Bitwise Operators
48
© 2007 Pearson Education, Inc. All rights reserved.
Portability Tip 10.6
Bitwise data manipulations are machine dependent.
49
© 2007 Pearson Education, Inc. All rights reserved.
50
Fig. 10.6 | Bitwise operators.
© 2007 Pearson Education, Inc. All rights reserved.
51
Outline
fig10_07.c
(1 of 2 )
© 2007 Pearson Education, Inc. All rights reserved.
52
Outline
fig10_07.c
(2 of 2 )
displayMask is a 1 followed by 31 zeros
Bitwise AND returns nonzero if the leftmost bits of displayMask and value are both 1, since all other bits in displayMask are 0s.
© 2007 Pearson Education, Inc. All rights reserved.
Common Programming Error 10.11
Using the logical AND operator (&&) for the bitwise AND operator (&) and vice versa is an error.
53
© 2007 Pearson Education, Inc. All rights reserved.
54
Fig. 10.8 | Results of combining two bits with the bitwise AND operator &.
© 2007 Pearson Education, Inc. All rights reserved.
55
Fig. 10.11 | Results of combining two bits with the bitwise inclusive OR operator |.
© 2007 Pearson Education, Inc. All rights reserved.
56
Fig. 10.12 | Results of combining two bits with the bitwise exclusive OR operator ^.
© 2007 Pearson Education, Inc. All rights reserved.
57
Outline
fig10_09.c
(1 of 3 )
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.
58
Outline
fig10_09.c
(2 of 3 )
Bitwise inclusive OR sets each bit in the result to 1 if at least one of the corresponding bits in the operands is 1
Bitwise exclusive OR sets each bit in the result to 1 if only one of the corresponding bits in the operands is 1
Complement operator sets each bit in the result to 0 if the corresponding bit in the operand is 1 and vice versa
© 2007 Pearson Education, Inc. All rights reserved.
59
Outline
fig10_09.c
(3 of 3 )
© 2007 Pearson Education, Inc. All rights reserved.
60
Outline
fig10_10.c
© 2007 Pearson Education, Inc. All rights reserved.
Common Programming Error 10.12
Using the logical OR operator (||) for the bitwise OR operator (|) and vice versa is an error.
61
© 2007 Pearson Education, Inc. All rights reserved.
62
Outline
fig10_13.c
(1 of 3 )
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.
63
Outline
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
© 2007 Pearson Education, Inc. All rights reserved.
64
Outline
fig10_13.c
(3 of 3 )
© 2007 Pearson Education, Inc. All rights reserved.
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.
65
© 2007 Pearson Education, Inc. All rights reserved.
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.
66
© 2007 Pearson Education, Inc. All rights reserved.
67
Fig. 10.14 | The bitwise assignment operators.
© 2007 Pearson Education, Inc. All rights reserved.
68
Fig. 10.15 | Operator precedence and associativity. (Part 1 of 2.)
© 2007 Pearson Education, Inc. All rights reserved.
69
Fig. 10.15 | Operator precedence and associativity. (Part 2 of 2.)
© 2007 Pearson Education, Inc. All rights reserved.
10.10 Bit Fields
struct BitCard {
unsigned face : 4;
unsigned suit : 2;
unsigned color : 1;
};
70
© 2007 Pearson Education, Inc. All rights reserved.
10.10 Bit Fields
struct Example {
unsigned a : 13;
unsigned : 3;
unsigned b : 4;
}
71
© 2007 Pearson Education, Inc. All rights reserved.
Performance Tip 10.3
Bit fields help conserve storage.
72
© 2007 Pearson Education, Inc. All rights reserved.
73
Outline
fig10_16.c
(1 of 2 )
Bit fields determine how much memory each member of a structure can take up
© 2007 Pearson Education, Inc. All rights reserved.
74
Outline
fig10_16.c
(2 of 2 )
© 2007 Pearson Education, Inc. All rights reserved.
75
Outline
© 2007 Pearson Education, Inc. All rights reserved.
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.
76
© 2007 Pearson Education, Inc. All rights reserved.
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.”
77
© 2007 Pearson Education, Inc. All rights reserved.
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).
78
© 2007 Pearson Education, Inc. All rights reserved.
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 space–time trade-offs that occur in computer science.
79
© 2007 Pearson Education, Inc. All rights reserved.
10.11 Enumeration Constants
enum Months { JAN = 1, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC};
80
© 2007 Pearson Education, Inc. All rights reserved.
81
Outline
fig10_18.c
(1 of 2 )
Enumeration sets the value of constant JAN to 1 and the following constants to 2, 3, 4…
© 2007 Pearson Education, Inc. All rights reserved.
82
Outline
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.
Common Programming Error 10.16
Assigning a value to an enumeration constant after it has been defined is a syntax error.
83
© 2007 Pearson Education, Inc. All rights reserved.
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.
84
© 2007 Pearson Education, Inc. All rights reserved.