Structures and Union in C Programming
Structures and Union in C Programming
1
0
C Structures, Unions,
Bit Manipulations and
Enumerations
OBJECTIVES
In this chapter you will learn:
To create and use structures, unions and
enumerations.
To pass structures to functions by value and
by reference.
To manipulate data with the bitwise operators.
To create bit fields for storing data compactly.
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
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
Fig. 10.1 | Possible storage alignment for a variable of type struct example showing an
undefined area in memory.
Ace of Spades
Ace of Spades
Ace of Spades
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
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;
10.8 Unions
Valid union operations
– Assignment to union of same type: =
– Taking address: &
– Accessing union members: .
– Accessing members using pointers: ->
double:
-92559592117433136000000000000000000000000000000000000000000000.000000
double:
100.000000
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 The bits in the result are set to 1 if at least one of the corresponding bits
OR in the two operands is 1.
^ bitwise exclusive The bits in the result are set to 1 if exactly one of the corresponding bits
OR 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.
~ one’s complement All 0 bits are set to 1 and all 1 bits are set to 0.
Fig. 10.8 | Results of combining two bits with the bitwise AND operator &.
Fig. 10.11 | Results of combining two bits with the bitwise inclusive OR operator |.
Fig. 10.12 | Results of combining two bits with the bitwise exclusive OR operator ^.
1 January (2 of 2 )
2 February
3 March Like symbolic constants, enumeration constants
4 April
5 May
are replaced by their values at compile time
6 June
7 July
8 August
9 September
10 October
11 November
12 December