Professional Documents
Culture Documents
04 Struct Union
04 Struct Union
Administrivia
Assignment 1 is due tonight Textbook
Lectures begin covering material that is also
Cox
&b'ectives
(e able to use com)ound data structures in )rograms (e able to use com)ound data structures as %unction arguments either by value or by re%erence (e able to do sim)le bit*vector mani)ulations
Cox
Structures
Com)ound data+ A date is
an int month and an int day and an int year
struct ADate { int month; int day; int year; }; struct ADate date; date.month = 9; date.day = 1; date.year = 2005; Unlike ,ava- C doesn.t automatically de%ine %unctions %or initiali/ing and )rinting 0
Cox
struct %har%har&nt { char c1; char c2; int i; } foo; foo.c1 = 'a'; foo.c2 = '('; foo.i = 0)D"AD$""#;
c1
c2
padding
!1
!2
"#
$"
AD
D"
Ty)ede%
6echanism %or creating ne7 ty)e names
8e7 names are an alias %or some other ty)e 9m)roves clarity o% the )rogram ty*edef +on, int!-.t; ty*edef struct ADate { int month; int day; int year; } Date; int!-.t i = 100000000000; Date d = {9/ 1/ 2005}; Overload existing type names for clarity and portability
Cox
Constants
Allo7 consistent use o% the same constant throughout the )rogram
9m)roves clarity o% the )rogram 1educes likelihood o% sim)le errors :asier to u)date constants in the )rogram Constant names are Preprocessor capitalized by directive convention 4define 5&6" 10 Define once, use int array0101; int array05&6"1; throughout the program for (i=0; i210; i33 { for (i=0; i25&6"; i33 { } }
Cox Structures and Unions 7
Arrays o% Structures
Array declaration
Date (irthdays07#8&"7D51; (oo+ chec9.(irthday(Date today { int i; for (i = 0; i 2 7#8&"7D5; i33 { if ((today.month == (irthdays0i1.month (today.day == (irthdays0i1.day return (true ; } return (fa+se ; }
Cox Structures and Unions 8
Constant
5ointers to Structures
Date create.date1(int month/ int day/ int year { Date d; d.month = month; d.day = day; d.year = year; return (d ; } ;oid create.date2(Date <d/ int month/ Pass-by-reference int day/ int year { d=>month = month; d=>day = day; d=>year = year; } Date today; today = create.date1(9/ -/ 200? ; create.date2(:today/ 9/ -/ 200? ;
9
Copies date
Cox
x$ A# x$ A" x$ A x$ %#
200? 9 0)1000
x! x! x!
# "
200? 9
10
Cox
11
Abstraction in C
From the #include file widget.h:
struct Bid,et; struct Bid,et <Bid,et.create(;oid ; int Bid,et.o*(struct Bid,et <Bid,et/ int o*erand ; ;oid Bid,et.destory(struct Bid,et <Bid,et ;
Definition is hidden(
(it*7ise o)erations+
(it*7ise (it*7ise (it*7ise (it*7ise A8<+ &1+ 8&T+ =&1+ 00100101 : 10111100 00100101 E 10111100 F 00100101 00100101 G 10111100 == == == == 00100100 10111101 11011010 10011001
Cox
13
.esult / '
000 0101 == 001 0101 : 000 0111
Cox
14
.esult / '
Cox
15
.esult / '
Cox
16
(it*%ield Structures
S)ecial syntax )acks structure values more tightly Similar to bit vectors- but arguably easier to read
8onetheless- bit vectors
struct #+a,s { int unsi,ned int unsi,ned int } foo; foo.f1 = =2; foo.f2 = 1; foo.fA = 2;
f1
f2
fA
s)eci%ic.
1 1 0 1 1 0
Cox
17
Unions
Choices+ An element is
an int i or a char c
union An"+t { int i; char c; } e+t1/ e+t2; e+t1.i = -; e+t2.c = 'a'; e+t2.i = 0)D"AD$""#;
sizeof(union 3
maximum o% sizeof(%ield
padding
"#
$"
i
AD
D"
Cox
18
Unions
A union value doesn.t >kno7? 7hich case it contains
union An"+t { int i; char c; } e+t1/ e+t2; e+t1.i = -; e+t2.c = 'a'; e+t2.i = 0)D"AD$""#; if (e+t1 currently has a char
?
0o+ should your program 1eep trac1 +hether e+t1, e+t2 hold an int or a char'
'
Cox
19
Tagged Unions
Tag every value 7ith its case 9.e.- )air the ty)e in%o together 7ith the union
9m)licit in ,ava- Scheme- 6L- 0
enum Inion.Ja, {&5.&7J/ &5.%KA8}; struct Ja,,edInion { enum Inion.Ja, ta,; union { int i; char c; } data; };
Cox
20
8ext Time
6emory Allocation
Cox
21