struct union

Upload: raghu-raman

Post on 03-Jun-2018

233 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/11/2019 Struct Union

    1/21

    Structures and Unions in C

    Scott Rixner

    [email protected]

  • 8/11/2019 Struct Union

    2/21

  • 8/11/2019 Struct Union

    3/21

    )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 byre(erence

    +e able to do sim,le bit-vector mani,ulations

    Cox / Rixner Structures and Unions 3

  • 8/11/2019 Struct Union

    4/21

    Cox / Rixner Structures and Unions 4

    Structures

    Com,ound data

    A date isan int month andan int day and

    an int year

    Unlike /ava0 C doesn tautomatically 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;

  • 8/11/2019 Struct Union

    5/21

    Cox / Rixner Structures and Unions 5

    Structure Re,resentation 4 Si2e

    sizeof(struct 5sum o( sizeof( (ield6 alignment ,adding

    7rocessor- and com,iler-s,eci(ic

    !2!1 "# $" AD D"c1 c2 ipadding

    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

  • 8/11/2019 Struct Union

    6/21

    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 andportability

    Simplify complex typenames

  • 8/11/2019 Struct Union

    7/21

    Cox / Rixner Structures and Unions 7

    Constants

    Allo9 consistent use o( the same constantthroughout the ,rogram;m,roves clarity o( the ,rogramReduces likelihood o( sim,le errors

  • 8/11/2019 Struct Union

    8/21

    Cox / Rixner Structures and Unions 8

    Arrays o( Structures

    Date irthdays 7#8&"7D ;

    oo+chec irthday(Date today

    { int i;

    for (i = 0; i 7#8&"7D ; i33 { if ((today.month == irthdays i .month :: (today.day == irthdays i .day return (true ; }

    return (fa+se ;}

    ConstantArray declaration

    Array index, thenstructure field

  • 8/11/2019 Struct Union

    9/21

    Cox / Rixner Structures and Unions 9

    7ointers to Structures

    Datecreate date1(int month/ int day/ int year{ Date d;

    d.month = month; d.day = day; d.year = year;

    return (d ;}

    oidcreate date2(Date 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- ;

  • 8/11/2019 Struct Union

    10/21

    Cox / Rixner Structures and Unions 10

    7ointers to Structures $cont.&

    oid

    create date2(Date month = month; d >day = day; d >year = year;}

    oidfun @ith dates( oid{ Date today; create date2(:today/ 2/ 5/ 201- ;}

    today.month

    today.day

    today.year

    x!

    x! "

    x! #

    month 2

    day 5

    year 201-

    x$ A

    x$ A"

    x$ A#

    d 0)1000x$ %#

    2

    5

    201-

  • 8/11/2019 Struct Union

    11/21

    Cox / Rixner Structures and Unions 11

    7ointers to Structures $cont.&

    Date day = day; d >year = year;

    return (d ;}

    &hat is d pointing to'('(

    )more on this later*

  • 8/11/2019 Struct Union

    12/21

    Cox Structures and Unions 12

    Abstraction in C

    struct @id,et;

    struct @id,et

  • 8/11/2019 Struct Union

    13/21

    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 10111100 == 10111101+it-9ise :)T E 00100101 == 11011010+it-9ise ?)R 00100101 F 10111100 == 10011001

  • 8/11/2019 Struct Union

    14/21

    Cox / Rixner Structures and Unions 14

    ),erations on +it =ectors

    const unsi,ned int +o@ three its mas = 0)G;unsi,ned int it ec = 0)15;

    000 0111001 0101

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

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

    000 0101 == 001 0101 : 000 0111

    im*ortant its = it ec : +o@ three its mas ;

    Selecting bits

    .esult / '

  • 8/11/2019 Struct Union

    15/21

    Cox / Rixner Structures and Unions 15

    ),erations on +it =ectors

    const unsi,ned int +o@ three its mas = 0)G;unsi,ned int it ec = 0)15;

    000 0111001 0101

    it ec = +o@ three its mas ;

    Setting bits

    .esult / '

    001 0111 == 001 0101 000 0111

  • 8/11/2019 Struct Union

    16/21

    Cox / Rixner Structures and Unions 16

    ),erations on +it =ectors

    const unsi,ned int +o@ three its mas = 0)G;unsi,ned int it ec = 0)15;

    000 0111001 0101

    it ec := E+o@ three its mas ;

    Clearing bits

    .esult / '

    001 0000 == 001 0101 : E000 0111

  • 8/11/2019 Struct Union

    17/21

    Cox / Rixner Structures and Unions 17

    +it-(ield Structures

    S,ecial syntax ,acksstructure values moretightly

    Similar to bit vectors0 but

    arguably easier to read:onetheless0 bit vectorsare more commonlyused.

    7added to be an integralnumber o( 9ords7lacement is com,iler-s,eci(ic.

    1 1 0 1 1 0 f1 f2 f?

    struct #+a,s { int f1 ?; unsi,ned int f2 1; unsi,ned int f? 2;} my f+a,s;

    my f+a,s.f1 = 2;my f+a,s.f2 = 1;my f+a,s.f? = 2;

  • 8/11/2019 Struct Union

    18/21

    Cox / Rixner Structures and Unions 18

    Unions

    Choices

    An element isan int i ora char c

    sizeof(union 5maximum 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$""#;

  • 8/11/2019 Struct Union

    19/21

    Cox / Rixner Structures and Unions 19

    Unions

    A union value doesn t kno9 9hich case itcontains

    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 1eeptrac1 +hether e+t1 , e+t2 hold

    an int or a char '

    ?

    ?2asic ans+er Another variable

    holds that info

  • 8/11/2019 Struct Union

    20/21

    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, {& &7I/ & %JA8};struct Ia,,edHnion { enum Hnion Ia, ta,; union { int i; char c; } data;};

  • 8/11/2019 Struct Union

    21/21

    Cox / Rixner Structures and Unions 21

    :ext Time

    8emory Allocation