Escolar Documentos
Profissional Documentos
Cultura Documentos
10.1
10.2
10.3
10.4
10.5
10.6
10.7
10.8
10.9
10.10
10.11
Introduction
Structure Definitions
Initializing Structures
Accessing Members of Structures
Using Structures with Functions
typedef
Example: High-Performance Card Shuffling and Dealing Simulation
Unions
Bitwise Operators
Bit Fields
Enumeration Constants
10.1 Introduction
Structures
Collections of related variables (aggregates) under one name
Can contain variables of different data types
Declarations
Declared like other variables:
card oneCard, deck[ 52 ], *cPtr;
struct card {
char *face;
char *suit;
} oneCard, deck[ 52 ], *cPtr;
2000 Prentice Hall, Inc. All rights reserved.
Assignment statements
Example:
card threeHearts = oneCard;
card threeHearts;
threeHearts.face = Three;
threeHearts.suit = Hearts;
myCardPtr->suit is equivalent to
( *myCardPtr ).suit
10.6 typedef
typedef
Creates synonyms (aliases) for previously defined data types
Use typedef to create shorter type names
Example:
typedef struct Card *CardPtr;
10
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
30
Outline
1. Load headers
1.1 Define struct
1.2 Function
prototypes
1.3 Initialize deck[]
and face[]
1.4 Initialize suit[]
11
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
59
60
Outline
12
2. fillDeck
2.1 shuffle
remainder (modulus).
for ( i = 0; i <= 51; i++ ) {
wDeck[ i ].face = wFace[ i % 13 ];
wDeck[ i ].suit = wSuit[ i / 13 ];
}
3. Function definitions
}
void shuffle( Card * const wDeck )
{
int i, j;
Card temp;
for ( i = 0; i <= 51; i++ ) {
j = rand() % 52;
temp = wDeck[ i ];
wDeck[ i ] = wDeck[ j ];
wDeck[ j ] = temp;
}
}
2000 Prentice Hall, Inc. All rights reserved.
61
62 void deal( const Card * const wDeck )
63 {
64
3. Function
definitions
Cycle through
array and
print
out data.
int i;
65
66
67
68
wDeck[ i ].suit,
69
( i + 1 ) % 2 ? '\t' : '\n' );
70 }
Outline
13
Eight
Eight
Seven
Ace
Deuce
Seven
Jack
King
Three
Three
Ten
Ten
Six
Six
Nine
Jack
King
Nine
Six
Queen
Ace
King
King
Queen
Four
Four
of
of
of
of
of
of
of
of
of
of
of
of
of
of
of
of
of
of
of
of
of
of
of
of
of
of
Diamonds
Clubs
Hearts
Clubs
Spades
Spades
Clubs
Hearts
Hearts
Clubs
Hearts
Clubs
Clubs
Hearts
Diamonds
Spades
Diamonds
Spades
Spades
Diamonds
Spades
Clubs
Spades
Hearts
Spades
Clubs
Ace
Five
Deuce
Ten
Six
Deuce
Ten
Jack
Three
Nine
Deuce
Seven
Queen
Three
Ace
Five
Seven
Four
Eight
Five
Nine
Five
Four
Eight
Jack
Queen
of
of
of
of
of
of
of
of
of
of
of
of
of
of
of
of
of
of
of
of
of
of
of
of
of
of
Hearts
Spades
Diamonds
Diamonds
Diamonds
Clubs
Spades
Diamonds
Diamonds
Clubs
Hearts
Diamonds
Spades
Spades
Diamonds
Clubs
Clubs
Hearts
Spades
Diamonds
Hearts
Hearts
Diamonds
Hearts
Hearts
Clubs
Outline
Program Output
14
10.8 Unions
union
union declarations
Same as struct
union Number {
int x;
float y;
};
union Number value;
2000 Prentice Hall, Inc. All rights reserved.
15
10.8 Unions
Valid union operations
16
Outline
An example of a union */
#include <stdio.h>
1. Define union
4
5
union number {
int x;
double y;
};
2. Set variables
9
10 int main()
11 {
12
13
14
value.x = 100;
15
printf( "%s\n%s\n%s%d\n%s%f\n\n",
16
17
18
"int:
19
"double:\n", value.y );
", value.x,
20
21
value.y = 100.0;
22
printf( "%s\n%s\n%s%d\n%s%f\n",
23
24
25
"int:
26
"double:\n", value.y );
27
", value.x,
return 0;
28 }
3. Print
17
Outline
Program Output
18
Name
Description
bitwise AND
bitwise OR
bitwise exclusive OR
<<
left shift
>>
right shift
Ones complement
19
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
30
Outline
1. Function prototype
number1 = 139;
number2 = 199;
printf( "\nThe result of combining the following\n" );
2000 Prentice Hall, Inc. All rights reserved.
20
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
59
60
displayBits( number1 );
displayBits( number2 );
printf( "using the bitwise exclusive OR operator ^ is\n" );
displayBits( number1 ^ number2 );
number1 = 21845;
printf( "\nThe one's complement of\n" );
displayBits( number1 );
printf( "is\n" );
displayBits( ~number1 );
Outline
2.1 Print
3. Function definition
return 0;
}
void displayBits( unsigned value )
{
unsigned c, displayMask = 1 << 31;
printf( "%7u = ", value );
for ( c = 1; c <= 32; c++ ) {
putchar( value & displayMask ? '1' : '0' );
value <<= 1;
if ( c % 8 == 0 )
putchar( ' ' );
}
putchar( '\n' );
}
2000 Prentice Hall, Inc. All rights reserved.
21
following
00000000 00001111
00000000 11110001
OR operator | is
00000000 11111111
following
00000000 10001011
00000000 11000111
OR operator ^ is
00000000 01001100
Outline
Program Output
22
struct BitCard {
unsigned face : 4;
unsigned suit : 2;
unsigned color : 1;
};
2000 Prentice Hall, Inc. All rights reserved.
23
Unnamed bit field with zero width aligns next bit field to a
new storage unit boundary
24
Example:
enum Months { JAN = 1, FEB, MAR, APR, MAY, JUN,
JUL, AUG, SEP, OCT, NOV, DEC};
25
Outline
26
#include <stdio.h>
4
5
7
8
int main()
{
enum months month;
11
12
13
14
"September", "October",
15
"November", "December" };
16
for ( month = JAN; month <= DEC; month++ )
18
19
20
return 0;
21 }
10
17
1. Define enumeration
2.1 Print
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
Program Output
27