Structures and Unions in C: Alan L. Cox Alc@cs - Rice.edu
Structures and Unions in C: Alan L. Cox Alc@cs - Rice.edu
Administrivia
Assignment 1 is due tonight Textbook
Lectures begin covering material that is also
covered by the textbook on 2/1 Assignment 3 (assigned 2/3) requires use of the textbook
Cox
Objectives
Be able to use compound data structures in programs Be able to use compound data structures as function arguments either by value or by reference Be able to do simple bit-vector manipulations
Cox
Structures
Compound data:
struct ADate { int month; int day; int year; }; struct ADate date; date.month = 9; date.day = 1; date.year = 2005; Unlike Java & Scheme, C doesnt automatically define functions for printing,
A date is
an int month and an int day and an int year
Cox
alignment padding
Processor- and compiler-specific
c1
c2
padding
61
62
EF
BE
AD
DE
Typedef
Mechanism for creating new type names
New names are an alias for some other type
Cox
Constants
Allow consistent use of the same constant throughout the program
Improves clarity of the program
Reduces likelihood of simple errors Easier to update constants in the program Constant names are Preprocessor directive capitalized by convention #define SIZE 10 int array[10]; for (i=0; i<10; i++) { }
Cox
int array[SIZE];
Arrays of Structures
Array declaration
Date birthdays[NFRIENDS]; bool check_birthday(Date today) { int i; for (i = 0; i < NFRIENDS; i++) { if ((today.month == birthdays[i].month) && (today.day == birthdays[i].day)) return (true); } return (false); }
Cox Structures and Unions 8
Constant
Pointers to Structures
Date create_date1(int month, int day, int year) { Date d; void create_date2(Date *d, int month, Pass-by-reference int day, int year) { d->month = month; d->day = day; d->year = year; }
Copies date
Cox
year:
2008
day:
month: d:
4
9
0x1000
today.year:
2008
today.day:
today.month:
4
9
10
Cox
11
Bit-wise operations:
Bit-wise Bit-wise Bit-wise Bit-wise AND: OR: NOT: XOR: 00100101 & 10111100 00100101 | 10111100 ~ 00100101 00100101 ^ 10111100 == == == == 00100100 10111101 11011010 10011001
Cox
12
A mask indicates which bit positions we are interested in Always use Cs unsigned types for bit vectors
Selecting bits:
important_bits = bit_vec & low_three_bits_mask;
Result = ?
000 0101 == 001 0101 & 000 0111
Cox
13
Setting bits:
bit_vec |= low_three_bits_mask; 001 0111 == 001 0101 | 000 0111
Result = ?
Cox
14
Clearing bits:
bit_vec &= ~low_three_bits_mask; 001 0000 == 001 0101 & ~000 0111
Result = ?
Cox
15
Bit-field Structures
Special syntax packs structure values more tightly Similar to bit vectors, but arguably easier to read
Nonetheless, bit vectors
struct Flags { int unsigned int unsigned int } foo; foo.f1 = -2; foo.f2 = 1; foo.f3 = 2;
f1
f2
f3
specific.
1 1 0 1 1 0
Cox
16
Unions
Choices:
union AnElt { int i; char c; } elt1, elt2; elt1.i = 4; elt2.c = a; elt2.i = 0xDEADBEEF;
An element is
an int i or a char c
sizeof(union ) =
maximum of sizeof(field)
padding
EF
BE
i
AD
DE
Cox
17
Unions
A union value doesnt know which case it contains
union AnElt { int i; char c; } elt1, elt2; elt1.i = 4; elt2.c = a; elt2.i = 0xDEADBEEF; if (elt1 currently has a char)
?
How should your program keep track whether elt1, elt2 hold an int or a char?
Cox
18
Tagged Unions
Tag every value with its case I.e., pair the type info together with the union
Implicit in Java, Scheme, ML,
enum Union_Tag {IS_INT, IS_CHAR}; struct TaggedUnion { enum Union_Tag tag; union { int i; char c; } data; };
Cox
19
Next Time
Memory Allocation
Cox
20