Title: Derived Types
1Chapter 12
Derived Types-- Enumerated, Structure and Union
These slides are intended for students at IUPUI
who are users of the book B.A. Forouzan, R.F.
Gilberg, Computer Science A Structured Approach
Using C, Brooks/Cole, second edition, 2001, ISBN
0-534-37482-4. Additions have been made to
existing slides from Brooks/Cole and some new
slides have been added by Kris A. Dines, Ph.D.,
Indiana University-Purdue University (IUPUI)
(kdines_at_iupui.edu) for a C course in Electrical
Engineering. Please see Brooks/Cole website
(www.brookscole.com) to download original slides.
2Derived Types
- Those made from existing types.
- Arrays are collections of existing types
- int array11,2,3
- Pointers are address locations of existing types
- float x
- float pxx
- Can also define our own groups of variables and
give that group a name. (structs)
3Figure 12-1 Derived types available in C
4Figure 12-2 Type definition using typedef
typedef int Int32 // Integer of specific
size (32-bit machine) Int32 ivalue
// New type used as any other Int32
pValueivalue
Identifier (better)
5More typedef Examples
- String type
- typedef char String
- Use it String myString156
- Pointer type
- typedef int pInt
- int a
- pInt pa
6Figure 12-3 Enumerated type discrete set of
values with names
for only one variable
tag creates a type to use for defining many
variables
7Figure 12-4 Enumerated type set of constants
Symbolic names rather than integers red0,
white1,,, yellow4 (constants)
With tag enum color color0red
With typedef COLORS color0red
Use anywhere integers are used
8Reassigning Constant Integer Values of enum
Symbols
- Set to unique, but arbitrary integers
- enum TV fox11, abc5, cnn66
- Start at one and increment by one
- enum months Jan1,Feb, Mar, Apr,,, etc.
9Enumerated Type Example
- enum TV fox11, abc5, cnn66 // define it
- int main()
-
- enum TV myChannelcnn // TV variable is
myChannel - if(myChannelfox)
// Test for fox -
- printf(I am watching the world series\n)
-
- else
-
- printf(I am not watching the world series\n)
-
1012-3 Structures
- structure collection of related data elements
- various types
- represents some mathematical or problem domain
object. - field a data element in the structure
11Structures Representing Fractions
- struct fraction // a tagged structure
- // can reuse for many variables
- int numerator
- int denominator
-
- struct fraction frac1 // declare a fraction
variable - struct // structure variable declaration
- // just defines frac1 variable...cannot
reuse - int numerator
- int denominator
- frac1 // just defines frac1... would have
to - //
repeat for frac2, etc. ? useless
12Structures Representing Fractions (Contd.)
- struct fraction // a tagged structure
- // can reuse for many variables
- int numerator // (put in a header file)
- int denominator
-
- int main()
-
- struct fraction frac1 // Accessing fields
- frac1.numerator3 // 3/4
- frac1.denominator4
- //...
13typedefd Structure
- Recall
- typedef int Int32
- typedef struct fraction
-
- int numerator
- int denominator
- Fraction // new type
14Typedef Structures Representing Fractions
(Contd.)
- typedef struct fraction // a typedefd structure
- // can reuse for many variables
- int numerator // (put in a header file)
- int denominator
- Fraction
- int main()
-
- Fraction frac1 // Accessing fields
- frac1.numerator3 // 3/4
- frac1.denominator4
- //...
15Typedef Structures Representing Fractions
(Contd.) Use these
- typedef struct fraction // a typedefd structure
- // can reuse for many variables
- int numerator // (put in a header file)
- int denominator
- Fraction // traditional format
- typedef struct int numerator int
denominator Fraction // equivalent - OR
- typedef struct / can leave off tag if
typedefd/ -
- int numerator
- int denominator
- Fraction
Existing type
New type
16Figure 12-5
FILE fp // Pointer to a FILE structure
17Figure 12-10 struct format variations
18Figure 12-11 Initializing structures
1912-4 ACCESSING FIELDS IN STRUCTURES
- Each field is just a variable usable as such
- Fraction frac13,4 // the fraction ¾
- frac1.numerator3 // same as above...
- frac1.denominator4
dot is the member operator that digs into a
structure to get a variable then use it like any
other variable
20Multiply Fractions
- typedef struct
- int numerator
- int denominator FRACTION
- int main (void)
-
- /Local Definitions /
- FRACTION fr1
- FRACTION fr2
- FRACTION res
- / Statements read two fractions /
- printf("Write the first fraction in the form of
x/y ") - scanf ("d /d", fr1.numerator,
-
fr1.denominator) - // contd........
- // main() contd.........
- printf("Write second fraction in the form of
x/y ") - scanf ("d /d", fr2.numerator,
-
fr2.denominator) - // the actual multiply
- res.numerator fr1.numerator
fr2.numerator - res.denominator fr1.denominator
fr2.denominator - printf("\nThe result of d/d d/d is d/d",
- fr1.numerator, fr1.denominator,
- fr2.numerator, fr2.denominator,
- res.numerator, res.denominator)
- return 0
21Operations on Structures
- Assignment or copy is the only one except for
those you program yourself. - Fraction frac13,4 // 3/4
- Fraction frac21,2 // 1/2
- frac2frac1 // now frac2 holds 3/4
// element-by-element copy
22Figure 12-13
23Figure 12-14 Pointers to structures
ptr.x is WRONG dot takes precedence (ptr)
goes to where the structure lives
24Pointer Selection Operator( p-gtmember )
- Fraction frac13,4 //a Fraction initialized
- Fraction pFrac1 // pointer to a Fraction
- pFrac1frac1 // pointer has value
addressof(frac1) - // Equivalent ways to access member fields
- frac1.numerator17 // dot member operator
- (pFrac1).numerator17 // deref pointer then dot
into it - pFrac1-gtnumerator17 // selection
operator (preferred) - // e.g., mul numerators
- pFrac3-gtnumerator (pFrac1-gtnumerator)
(pFrac2-gtnumerator)
25LECTURE 9 END