You are on page 1of 21

Structures and Unions in C

Scott Rixner
rixner@rice.edu
Cox / Rixner Structures and Unions 2
Administrivia
Assignment 1 is due tonight
Textbook

Lectures begin covering material that is also
covered by the textbook on !"

Assignment # $assigned !%& re'uires use o( the
textbook
)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 / Rixner Structures and Unions 3
Cox / Rixner Structures and Unions 4
Structures
Com,ound data.
A date is

an int month and

an int day and

an int year
Unlike /ava0 C doesn1t
automatically de(ine (unctions
(or initiali2ing and ,rinting 3
struct ADate {
int month;
int day;
int year;
};
struct ADate date;
date.month = 9;
date.day = 1;
date.year = 2005;
Cox / Rixner Structures and Unions 5
Structure Re,resentation 4 Si2e
sizeof(struct … 5
sum o( sizeof((ield
6 alignment ,adding
7rocessor- and com,iler-s,eci(ic
!2 !1 "# $" AD D"
c1 c2 i padding
struct %har%har&nt {
char c1;
char c2;
int i;
} foo;
foo.c1 = 'a';
foo.c2 = '(';
foo.i = 0)D"AD$""#;
x86 uses “little-endian” representation
Cox / Rixner Structures and Unions 6
Ty,ede(
8echanism (or creating ne9 ty,e names

:e9 names are an alias (or some other ty,e

;m,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
Simplify complex type
names
Cox / Rixner Structures and Unions 7
Constants
Allo9 consistent use o( the same constant
throughout the ,rogram

;m,roves clarity o( the ,rogram

Reduces likelihood o( sim,le errors

<asier to u,date constants in the ,rogram
int array0101;
for (i=0; i210; i33 {

}
4define 5&6" 10
int array05&6"1;
for (i=0; i25&6"; i33 {

}
Preprocessor
directive
Constant names are
capitalized by
convention
Define once,
use
throughout
the program
Cox / Rixner Structures and Unions 8
Arrays o( Structures
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;
}
Constant
Array declaration
Array index, then
structure field
Cox / Rixner Structures and Unions 9
7ointers 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/
int day/
int year
{
d=>month = month;
d=>day = day;
d=>year = year;
}
Copies date
Pass-by-reference
Date today;
today = create.date1(1/ 2?/ 201-;
create.date2(:today/ 1/ 2?/ 201-;
Cox / Rixner Structures and Unions 10
7ointers to Structures $cont.&
;oid
create.date2(Date <d/
int month/
int day/
int year
{
d=>month = month;
d=>day = day;
d=>year = year;
}
;oid
fun.@ith.dates(;oid
{
Date today;
create.date2(:today/ 2/ 5/ 201-;
}
today.monthA
today.dayA
today.yearA
x!
x!"
x!#
monthA 2
dayA 5
yearA 201-
x$A
x$A"
x$A#
dA 0)1000
x$%#
2
5
201-
Cox / Rixner Structures and Unions 11
7ointers to Structures $cont.&
Date <
create.date?(int month/
int day/
int year
{
Date <d;
d=>month = month;
d=>day = day;
d=>year = year;
return (d;
}
&hat is d pointing to'('(
)more on this later*
Cox Structures and Unions 12
Abstraction in C
struct @id,et;
struct @id,et <@id,et.create(;oid;
int @id,et.o*(struct @id,et <@id,et/ int o*erand;
;oid @id,et.destory(struct @id,et <@id,et;
From the #include file widget.h:
From the file widget.c:
4inc+ude B@id,et.hC
struct @id,et {
int );

};
Definition is hidden(
Cox / Rixner Structures and Unions 12
Cox / Rixner Structures and Unions 13
Collections o( +ools $+it =ectors&
+yte0 9ord0 ... can re,resent many booleans
)ne ,er bit0 e.g.0 00100101 5 (alse0 (alse0 true0 ...0 true
+it-9ise o,erations.
+it-9ise A:>. 00100101 : 10111100 == 00100100
+it-9ise )R. 00100101 D 10111100 == 10111101
+it-9ise :)T. E 00100101 == 11011010
+it-9ise ?)R. 00100101 F 10111100 == 10011001
Cox / Rixner Structures and Unions 14
),erations on +it =ectors
const unsi,ned int +o@.three.(its.mas9 = 0)G;
unsi,ned int (it.;ec = 0)15;
0…00 0111
0…01 0101
Al+ays use C,s unsi,ned types for bit vectors
A mask indicates +hich bit positions +e are interested in
0…00 0101 == 0…01 0101 : 0…00 0111
im*ortant.(its = (it.;ec : +o@.three.(its.mas9;
Selecting bits-
.esult / '
Cox / Rixner Structures and Unions 15
),erations on +it =ectors
const unsi,ned int +o@.three.(its.mas9 = 0)G;
unsi,ned int (it.;ec = 0)15;
0…00 0111
0…01 0101
(it.;ec D= +o@.three.(its.mas9;
Setting bits-
.esult / '
0…01 0111 == 0…01 0101 D 0…00 0111
Cox / Rixner Structures and Unions 16
),erations on +it =ectors
const unsi,ned int +o@.three.(its.mas9 = 0)G;
unsi,ned int (it.;ec = 0)15;
0…00 0111
0…01 0101
(it.;ec := E+o@.three.(its.mas9;
Clearing bits-
.esult / '
0…01 0000 == 0…01 0101 : E0…00 0111
Cox / Rixner Structures and Unions 17
+it-(ield Structures
S,ecial syntax ,acks
structure values more
tightly
Similar to bit vectors0 but
arguably easier to read

:onetheless0 bit vectors
are more commonly
used.
7added to be an integral
number o( 9ords

7lacement is com,iler-
s,eci(ic.
1 1 0 1 1 0 … …
f1 f2 f?
struct #+a,s {
int f1A?;
unsi,ned int f2A1;
unsi,ned int f?A2;
} my.f+a,s;
my.f+a,s.f1 = =2;
my.f+a,s.f2 = 1;
my.f+a,s.f? = 2;
Cox / Rixner Structures and Unions 18
Unions
Choices.
An element is

an int i or

a char c
sizeof(union … 5
maximum o( sizeof((ield
"# $" AD D"
c
i
padding
union An"+t {
int i;
char c;
} e+t1/ e+t2;
e+t1.i = -;
e+t2.c = 'a';
e+t2.i = 0)D"AD$""#;
Cox / Rixner Structures and Unions 19
Unions
A union value doesn1t @kno9A 9hich 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'
?
?
2asic ans+er- Another variable
holds that info
Cox / Rixner Structures and Unions 20
Tagged Unions
Tag every value 9ith its case
;.e.0 ,air the ty,e in(o together 9ith the union
;m,licit in /ava0 Scheme0 8L0 3
3num must be external to struct,
so constants are globally visible4
Struct field must be named4
enum Hnion.Ia, {&5.&7I/ &5.%JA8};
struct Ia,,edHnion {
enum Hnion.Ia, ta,;
union {
int i;
char c;
} data;
};
Cox / Rixner Structures and Unions 21
:ext Time
8emory Allocation