21
derived types

derived types

  • Upload
    kawena

  • View
    48

  • Download
    4

Embed Size (px)

DESCRIPTION

derived types. Derived Types . The type definition Enumerated types Accessing structures Complex structures Arrays of structures Structures and functions. Type Definition (typedef). Derived Types . Traditionally upper case. Keyword. standard or derived type. - PowerPoint PPT Presentation

Citation preview

Page 1: derived types

derived types

Page 2: derived types

• The type definition• Enumerated types• Accessing structures• Complex structures• Arrays of structures• Structures and functions

Derived Types

Page 3: derived types

Type Definition (typedef)• It gives the name to a data type by creating a new type that can

be used anywhere a type is permitted• Advantage

o Allows to replace a complex name such as a pointer declaration with mnemoic that makes the program easier to read and follow

o typedef type IDENTIFIER ;o e.g. o typedef int INTEGER;o char * stringPtrArray[20]; can be written as

typedef char *STRING; STRING stringPtrArray[20];

Derived Types

Keywordstandard or derived type

Traditionally upper case

Page 4: derived types

Enumerated types• enum is built on integer types• Each integer value is given an identifier called an enumeration

constant• It allows to use symbolic names rather than numbers which makes

our programs much more readable • Once enumerated types are defined, we can create variables from

standard types• C allows the enumerated constants or variables that hold

enumerated constants, to be used anywhere that integers can be used

• Keep enumerated types separate from integer types

Derived Types

Page 5: derived types

• enum { enumeration constants } variable_identifier;o Format 1 : Enumerated variable

• enum tag { enumeration constants };• enum tag variable_identifier;

o Format 2: enumerated tag• To use multiple enumerated types in a program, you need to

create an enumerated type• It includes a tag after the keyword enum; the tag is an enum

identifier• Example of enumeration

o enum months { jan, feb, mar,april,may};• It is list of one or more identifiers separated by commas

o Here jan equates to 0, feb to 1 and so ono enum months {jan=1, feb , jun=6, aug=1};o enum months birthmonth;

Derived Types

Enumerated types

Page 6: derived types

Enumerated type definition• enum colors{ red, white, blue};• enum colors acolor;

o Format 1 : Enumerated variable• typedef enum{ red,white, blue } COLORS;• COLORS aColor;

o Format 2: Enumerated typedef

Derived Types

Page 7: derived types

example• /* program to demonstrate the use of enum in switch*/• Main( )• {• enum items {laptop=1,cellphone,TV, ironbox};• int choice;• printf(“enter your choice”);• scanf(“%d”,&choice);• switch(choice)• {• case laptop: printf(“ you pressed laptop”);• break; • case cellphone:printf(“ you pressed cellphone”);• break; • case TV: printf(“ you pressed TV”);• break; • case ironbox: printf(“ you pressed ironbox”);• }• } • Here meaningful names are given in switch instead of

number.

Derived Types

Page 8: derived types

Structure format variation struct {

… …

} variable_identifier;-------------------------------------------------------------------------------- struct tag {

… …

} variable_identifier; struct tag variable_identifier; ----------------------------------------------------------------------------------------typedef

struct { … …

} TYPE_ID;• TYPE_ID variable_identifier;

Derived Types

Page 9: derived types

Example typedef struct { int x; int y; float t; char u; } SAMPLE; SAMPLE sam1; SAMPLE *ptr; ptr=&sam1; Derive

d Types

Page 10: derived types

Accessing structures• Each field in a structure can be accessed and manipulated using

expressions and operators• Period (.) is used to distinguish normal identifiers from the

members in the structureo e.g. If (employee1.gender = = ‘M’ ) employee1.salary +=employee1.hra;o scanf(“%d %f %c “, &sam1.empcode, &sam1.salary, &sam1.gender);o (.) precedence 17, postfix -16, unary increment -15o sam1.x++ and ++sam2.x are valid

Derived Types

Page 11: derived types

Structure operations• A structure can only be copied to another structure of the same

type using the assignment operator

Derived Types

Page 12: derived types

Pointers to structures• Define a pointer for the structure

o SAMPLE *ptr;o ptr=&sam1;o Structure can be accessed using indirection operator (*) refering to the

whole structureo E.g.

(*ptr ).x Brackets essential because precedence of member operator (17) is higher than

indirection operator (15) *ptr.x is interpreted as *(ptr.x) (*pointername ).fieldname same as pointername-> fieldname

Derived Types

Page 13: derived types

Complex structure• Structure within structure (nested structures), arrays within

structures, arrays of structures • Nesting must be done from inside out- innermost structure first,

then next level, working upward toward the outer most inclusive structure

• Each structure must be initialized completely before proceeding to the next number

• Each structure enclosed in the braces

Derived Types

Page 14: derived types

Defining arrays for structures• Structures can have more than one array as members. They can

be accessed either by indexing through pointers as long as they are properly qualified with member operators

• Like structures an array may be included within the structure or may be declared separately and then included

• If declared separately, the declaration must be complete before it can be used in the structure

• Regardless of how we declare the structure, each element will have the same reference. First to the structure and then to the array element

• When structure contains array we can use pointers to refer directly to the array elements

Derived Types

Page 15: derived types

Array initialization in structures• Since array is a separate member, its values must be included in

a separate set of braces• Use of pointers can save memory• e.g.

o char may[]=“May”o stamp.date.month=may;// assigns month “May” to the structure

Derived Types

Page 16: derived types

Array of structures• Create the array as done for normal array of integers

o E.g. o STUDENT stuary[50];o To access the midterm marks of one of the subject 1 for student 4, we

can writeo studAry[4].midterm[1];o The index operator the member operator, the selection operator have

same precedence and associativity is from left to right

Derived Types

Page 17: derived types

Structures and functions• For structures to be useful, we must be able to pass them to

functions and return them• A function can access the members of a structure in three ways

o Individual members can be passed to the functiono Whole structure can be passed and the function can access the member

using pass by valueo The address of a structure or member can be passed, and the function

can access the members through indirection and selection operators ( pass by address)

• We can send the individual elements or the whole structure to a function

• A function can also return a structure

Derived Types

Page 18: derived types

Passing structures through pointers• When structures are large, efficiency could suffer, especially with

heavily used function• You will find that structures are passed through pointers• It is common to pass structures thorugh pointers when the

structure is in dynamic memory• Selection operator () has higher precedence than the address

operator(&) and it can be coded without parenthesis• e.g.

&ptrnumerator &(*pFr).denominator // member operator has higher precedence over indirection operator and address operatorSince the address and member operator are the same level, we need to use the parenthesis only around the pointer dereference

Derived Types

Page 19: derived types

unions

Page 20: derived types

• A union is a construct that allows memory to be shared between different data types.

• Declaration syntax is similar to that of structure except the keyword struct to be replaced by union.

• union sharedData{• char chAry[2]; • short num; };• Both share the same location i.e chAry[0] is MSB byte of

num and chAry[1] is LSB byte of num.• Member accessing is similar to structures.

Derived Types

Page 21: derived types

• Only the first type declared in the union can be initialized while defining union variable.

• Other types can be read or assigned value using assignment operator.

• While initializing, values must be enclosed in {}• A structure member can be an union. And vice versa.• Example Derive

d Types