Structures Unions and Typedefs CS2301 System Programming for

  • Slides: 33
Download presentation
Structures, Unions, and Typedefs CS-2301, System Programming for Non-Majors (Slides include materials from The

Structures, Unions, and Typedefs CS-2301, System Programming for Non-Majors (Slides include materials from The C Programming Language, 2 nd edition, by Kernighan and Ritchie and from C: How to Program, 5 th and 6 th editions, by Deitel and Deitel) CS-2301, B-Term 2009 Structures, Unions, and Typedefs 1

Reading Assignment • Chapter 6 of Kernighan & Ritchie • Note § 6. 9,

Reading Assignment • Chapter 6 of Kernighan & Ritchie • Note § 6. 9, Bit Fields. – These are used frequently in embedded systems – When you need to communicate directly with the bits of certain registers – Much about them is implementation dependent – More later in the course CS-2301, B-Term 2009 Structures, Unions, and Typedefs 2

Structures and Unions • The last major language facility in C to be introduced

Structures and Unions • The last major language facility in C to be introduced in this course. • Essential for building up “interesting” data structures — e. g. , • Data structures of multiple values of different kinds • Data structures of indeterminate size • Essential for solving “interesting” problems • Most of the “real” problems in the C world CS-2301, B-Term 2009 Structures, Unions, and Typedefs 3

Definition — Structure • A collection of one or more variables, typically of different

Definition — Structure • A collection of one or more variables, typically of different types, grouped together under a single name for convenient handling • Known as struct in C and C++ CS-2301, B-Term 2009 a. Like s i a cla t b c u ut w r ss in s t d s o i th n Java , a eth + m o + met In C with no hod s s clas Structures, Unions, and 4 Typedefs

struct • Defines a new type • I. e. , a new kind of

struct • Defines a new type • I. e. , a new kind of data type that compiler regards as a unit • E. g. , struct motor { float volts; //voltage of the motor float amps; //amperage of the motor int phases; //# of phases of the motor float rpm; //rotational speed of motor }; //struct motor CS-2301, B-Term 2009 Structures, Unions, and Typedefs 5

struct pe • Defines a new type y t he t f o e

struct pe • Defines a new type y t he t f o e m • E. g. , Na Note: – name of type is optional struct motor { float volts; float amps; int phases; float rpm; }; //struct motor CS-2301, B-Term 2009 Structures, Unions, and Typedefs if you are just declaring a single struct (middle of p. 128) 6

struct • Defines a new type • E. g. , struct motor { float

struct • Defines a new type • E. g. , struct motor { float volts; float amps; int phases; float rpm; }; //struct motor Members of the struct A member of a struct is analogous to a field of a class in Java CS-2301, B-Term 2009 Structures, Unions, and Typedefs 7

Declaring struct variables struct motor p, q, r; • Declares and sets aside storage

Declaring struct variables struct motor p, q, r; • Declares and sets aside storage for three variables – p, q, and r – each of type struct motor M[25]; • Declares a 25 -element array of struct motor; allocates 25 units of storage, each one big enough to hold the data of one motor struct motor *m; • Declares a pointer to an object of type struct motor CS-2301, B-Term 2009 Structures, Unions, and Typedefs 8

Accessing Members of a struct • Let struct motor p; struct motor q[10]; •

Accessing Members of a struct • Let struct motor p; struct motor q[10]; • Then p. volts p. amps p. phases p. rpm — is the voltage — is the amperage — is the number of phases — is the rotational speed q[i]. volts q[i]. rpm — is the voltage of the ith motor — is the speed of the ith motor CS-2301, B-Term 2009 Structures, Unions, and Typedefs 9

Accessing Members of a struct (continued) • Let • s? e struct motor *p;

Accessing Members of a struct (continued) • Let • s? e struct motor *p; nth re a p e h t Then Why (*p). volts — is the voltage of the motor pointed to by p (*p). phases — is the number of phases of the motor pointed to by p CS-2301, B-Term 2009 Structures, Unions, and Typedefs 10

Accessing Members of a struct (continued) r o t a er p o ce

Accessing Members of a struct (continued) r o t a er p o ce ' n. e ' struct motor e *p; ed s c u e r a Bec igher p *' Then has h nary ' u n a (*p). volts — is the voltage of the motor pointed th to by p (*p). phases — is the number of phases of the motor pointed to by p • Let • CS-2301, B-Term 2009 Structures, Unions, and Typedefs 11

Accessing Members of a struct (continued) • Let struct motor *p; • Then —

Accessing Members of a struct (continued) • Let struct motor *p; • Then — is the voltage of the motor pointed Reason: – you really want the expression to by p m. volt * m. amps (*p). phases — is the number of phases of the to mean motor what you think ittoshould pointed by p mean! (*p). volts CS-2301, B-Term 2009 Structures, Unions, and Typedefs 12

Accessing Members of a struct (continued) • The (*p). member notation is a nuisance

Accessing Members of a struct (continued) • The (*p). member notation is a nuisance • Clumsy to type; need to match ( ) • Too many keystrokes • This construct is so widely used that a special notation was invented, i. e. , – p->member, where p is a pointer to the structure CS-2301, B-Term 2009 Structures, Unions, and Typedefs 13

Previous Example Becomes … • Let struct motor *p; • Then — is the

Previous Example Becomes … • Let struct motor *p; • Then — is the voltage of the motor pointed to by p p -> phases — is the number of phases of the motor pointed to by p p -> volts CS-2301, B-Term 2009 Structures, Unions, and Typedefs 14

Operations on struct • Copy/assign struct motor p, q; p = q; • Get

Operations on struct • Copy/assign struct motor p, q; p = q; • Get address struct motor p; struct motor *s s = &p; • Access members p. volts; s -> amps; CS-2301, B-Term 2009 Structures, Unions, and Typedefs 15

Operations on struct (continued) • Remember: – – Passing an argument by value is

Operations on struct (continued) • Remember: – – Passing an argument by value is an instance of copying or assignment – Passing a return value from a function to the caller is an instance of copying or assignment • E. g, : – struct motor f(struct motor g) { struct motor h = g; . . . ; return h; } CS-2301, B-Term 2009 Structures, Unions, and Typedefs 16

Assigning to a struct • K & R say (p. 131) – “If a

Assigning to a struct • K & R say (p. 131) – “If a large structure is to be passed to a function, it is generally more efficient to pass a pointer than to copy the whole structure” • I disagree: – – Copying is very fast on modern computers – Creating an object with malloc() and assigning a pointer is not as fast – Esp. if you want the object passed or returned by value – In real life situations, it is a judgment call CS-2301, B-Term 2009 Structures, Unions, and Typedefs 17

Initialization of a struct • Let struct motor { float volts; float amps; int

Initialization of a struct • Let struct motor { float volts; float amps; int phases; float rpm; }; //struct motor w e n a s e c a u d g o n r i ! struct motor m = {208, iz 3, g 1800}; nt 20, y l i l a i 9 t u 9 i n C y i l f u o initializes the struct tr y – a w uct str • Then • See also p. 133 for initializing arrays of structs CS-2301, B-Term 2009 Structures, Unions, and Typedefs 18

Why structs? • Open-ended data structures – E. g. , structures that may grow

Why structs? • Open-ended data structures – E. g. , structures that may grow during processing – Avoids the need for realloc() and a lot of copying • Self-referential data structures – Lists, trees, etc. CS-2301, B-Term 2009 Structures, Unions, and Typedefs 19

Example ! l a g s le i struct item { s i h

Example ! l a g s le i struct item { s i h T ! s e char *s; Y struct item *next; } • • I. e. , an item can point to another item … which can point to yet another item … etc. Thereby forming a list of items CS-2301, B-Term 2009 Structures, Unions, and Typedefs 20

Looking Ahead • The rest of this course is about data structures that you

Looking Ahead • The rest of this course is about data structures that you will typically encounter in C programs in your professional lives • All of them involve structs. CS-2301, B-Term 2009 Structures, Unions, and Typedefs 21

A note about structs and pointers • The following is legal: – /* in

A note about structs and pointers • The following is legal: – /* in a. c or. h file struct item; struct item *p, *q; Called an opaque type! */ Program can use pointers to items but cannot see into items. Cannot defined any items, cannot malloc any items, etc. … /* In another file */ struct item { Implementer of item can change the definition without forcing int member 1; users of pointers to change float member 2; their code! struct item *member 3; }; CS-2301, B-Term 2009 Structures, Unions, and Typedefs 22

Another note about structs • The following is not legal: – struct motor {

Another note about structs • The following is not legal: – struct motor { float volts; float amps; float rpm; unsigned int phases; }; //struct motor m; motor *p; CS-2301, B-Term 2009 You must write struct motor m; struct motor *p; Structures, Unions, and Typedefs 23

Typedef • Definition: – a typedef is a way of renaming a type –

Typedef • Definition: – a typedef is a way of renaming a type – See § 6. 7 ou ” y t s t c le u , r f t e s “ d typedef struct motor Motor; e d r p o y w t , e. E. g out th e v a e l Motor m, n; Motor *p, r[25]; Motor function(const Motor m; …); • E. g. , CS-2301, B-Term 2009 Structures, Unions, and Typedefs 24

typedef (continued) • typedef may be used to rename any type – – Convenience

typedef (continued) • typedef may be used to rename any type – – Convenience in naming Clarifies purpose of the type Cleaner, more readable code Portability across platforms • E. g. , – typedef char *String; • E. g. , – typedef int size_t; – typedef long int 32; – typedef long int 64; CS-2301, B-Term 2009 Structures, Unions, and Typedefs 25

typedef (continued) • typedef may be used to rename any type – – Convenience

typedef (continued) • typedef may be used to rename any type – – Convenience in naming Clarifies purpose of the type Cleaner, more readable code Portability across platforms • E. g. , – typedef char *String; • E. g. , m o + r f + e. C g d n n a cha C y n a i m rm! n o e o f e t r m h la code ile! tm p o e c o s t ble f ey h r h m e T r a. V latfo port n a p for nce i. Esp fined o De – typedef int size_t; – typedef long int 32; – typedef long int 64; CS-2301, B-Term 2009 Structures, Unions, and Typedefs 26

Revisit note about structs and pointers • The following is legal: – /* in

Revisit note about structs and pointers • The following is legal: – /* in a. c or. h file */ typedef struct _item Item; Item *p, *q; … /* In another file */ struct _item { char *info; Item *next. Item; }; CS-2301, B-Term 2009 Structures, Unions, and Typedefs 27

Questions about structs and pointers? CS-2301, B-Term 2009 Structures, Unions, and Typedefs 28

Questions about structs and pointers? CS-2301, B-Term 2009 Structures, Unions, and Typedefs 28

Unions • A union is like a struct, but only one of its members

Unions • A union is like a struct, but only one of its members is stored, not all • I. e. , a single variable may hold different types at different times • Storage is enough to hold largest member • Members are overlaid on top of each other • E. g. , union { int ival; float fval; char *sval; } u; CS-2301, B-Term 2009 Structures, Unions, and Typedefs 29

Unions (continued) • It is programmer’s responsibility to keep track of which type is

Unions (continued) • It is programmer’s responsibility to keep track of which type is stored in a union at any given time! • E. g. , (p. 148) struct tagged. Item { enum {i. Type, f. Type, c. Type} tag; union { int ival; float fval; char *sval; } u; }; CS-2301, B-Term 2009 Structures, Unions, and Typedefs 30

Unions (continued) • It is programmer’s responsibility to keep track of which type is

Unions (continued) • It is programmer’s responsibility to keep track of which type is stored in a union at any given time! • E. g. , (p. 148) struct tagged. Item { Members of struct are: – enum {i. Type, f. Type, c. Type} tag; enum tag; union { union u; int ival; float fval; Value of tag says which char *sval; member of u to use } u; }; CS-2301, B-Term 2009 Structures, Unions, and Typedefs 31

Unions (continued) • unions are used much less frequently than structs — mostly •

Unions (continued) • unions are used much less frequently than structs — mostly • in the inner details of operating system • in device drivers • in embedded systems where you have to access registers defined by the hardware CS-2301, B-Term 2009 Structures, Unions, and Typedefs 32

Questions? CS-2301, B-Term 2009 Structures, Unions, and Typedefs 33

Questions? CS-2301, B-Term 2009 Structures, Unions, and Typedefs 33