Download as pdf or txt
Download as pdf or txt
You are on page 1of 21

Structures and Unions in C

Alan L. Cox alc@rice.edu

Administrivia
Assignment 1 is due tonight Textbook
Lectures begin covering material that is also

covered by the textbook on 2 1 Assignment ! "assigned 2 !# re$uires use o% the textbook

Cox

Structures and Unions

&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 and Unions

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

Structures and Unions

Structure 1e)resentation 2 Si/e


sizeof(struct 3
4 sum o% sizeof(%ield alignment )adding
5rocessor* and com)iler*s)eci%ic

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"

x86 uses little-endian representation


Cox Structures and Unions 5

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

Simplify complex type names

Cox

Structures and Unions

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

Array index, then structure field


::

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

Structures and Unions

5ointers to Structures "cont.#


;oid create.date2(Date <d/ int month/ int day/ int year { d=>month = month; d=>day = day; d=>year = year; } ;oid foo(;oid { Date today; create.date2(:today/ 9/ -/ 200? ; }
Cox

x$ A# x$ A" x$ A x$ %#

year@ day@ month@ d@

200? 9 0)1000

x! x! x!

# "

today.year@ today.day@ today.month@

200? 9

Structures and Unions

10

5ointers to Structures "cont.#


Date < create.dateA(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

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(

From the file widget.c:


4inc+ude CBid,et.hD struct Bid,et { int ); };
Cox Structures and Unions 12

Collections o% (ools "(it ;ectors#


(yte- 7ord- ... can re)resent many booleans
&ne )er bit- e.g.- 00100101 3 %alse- %alse- true- ...- true

(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

Structures and Unions

13

&)erations on (it ;ectors


const unsi,ned int unsi,ned int +oB.three.(its.mas9 = 0)H; (it.;ec = 0)15; 000 0111 001 0101

A mask indicates +hich bit positions +e are interested in

Al+ays use C,s unsi,ned types for bit vectors

Selecting bitsim*ortant.(its = (it.;ec : +oB.three.(its.mas9;

.esult / '
000 0101 == 001 0101 : 000 0111

Cox

Structures and Unions

14

&)erations on (it ;ectors


const unsi,ned int unsi,ned int +oB.three.(its.mas9 = 0)H; (it.;ec = 0)15; 000 0111 001 0101

Setting bits(it.;ec E= +oB.three.(its.mas9; 001 0111 == 001 0101 E 000 0111

.esult / '

Cox

Structures and Unions

15

&)erations on (it ;ectors


const unsi,ned int unsi,ned int +oB.three.(its.mas9 = 0)H; (it.;ec = 0)15; 000 0111 001 0101

Clearing bits(it.;ec := F+oB.three.(its.mas9; 001 0000 == 001 0101 : F000 0111

.esult / '

Cox

Structures and Unions

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@A; f2@1; fA@2;

are more commonly used.

5added to be an integral number o% 7ords


5lacement is com)iler*

f1

f2

fA

s)eci%ic.

1 1 0 1 1 0

Cox

Structures and Unions

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

Structures and Unions

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'

'

2asic ans+er- Another variable holds that info

Cox

Structures and Unions

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

3num must be external to struct, so constants are globally visible4

Struct field must be named4

Structures and Unions

20

8ext Time
6emory Allocation

Cox

Structures and Unions

21

You might also like