210 likes | 349 Views
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.
E N D
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 It gives the name to a data type by creating a new type that can be used anywhere a type is permitted Advantage Allows to replace a complex name such as a pointer declaration with mnemoic that makes the program easier to read and follow typedef type IDENTIFIER ; e.g. typedef int INTEGER; char * stringPtrArray[20]; can be written as typedef char *STRING; STRING stringPtrArray[20];
Enumerated types Derived 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
Enumerated types Derived Types enum { enumeration constants } variable_identifier; Format 1 : Enumerated variable enum tag { enumeration constants }; enum tag variable_identifier; 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 enum months { jan, feb, mar,april,may}; It is list of one or more identifiers separated by commas Here jan equates to 0, feb to 1 and so on enum months {jan=1, feb , jun=6, aug=1}; enum months birthmonth;
Enumerated type definition Derived Types enum colors{ red, white, blue}; enum colors acolor; Format 1 : Enumerated variable typedef enum{ red,white, blue } COLORS; COLORS aColor; Format 2: Enumerated typedef
example Derived Types /* 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.
Structure format variation Derived Types struct { … … } variable_identifier; -------------------------------------------------------------------------------- struct tag { … … } variable_identifier; struct tag variable_identifier; ----------------------------------------------------------------------------------------typedef struct { … … } TYPE_ID; TYPE_ID variable_identifier;
Example Derived Types typedef struct { int x; int y; float t; char u; } SAMPLE; SAMPLE sam1; SAMPLE *ptr; ptr=&sam1;
Accessing structures Derived Types 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 structure e.g. If (employee1.gender = = ‘M’ ) employee1.salary +=employee1.hra; scanf(“%d %f %c “, &sam1.empcode, &sam1.salary, &sam1.gender); (.) precedence 17, postfix -16, unary increment -15 sam1.x++ and ++sam2.x are valid
Structure operations Derived Types A structure can only be copied to another structure of the same type using the assignment operator
Pointers to structures Derived Types Define a pointer for the structure SAMPLE *ptr; ptr=&sam1; Structure can be accessed using indirection operator (*) refering to the whole structure 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
Complex structure Derived Types 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
Defining arrays for structures Derived Types 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
Array initialization in structures Derived Types 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. char may[]=“May” stamp.date.month=may;// assigns month “May” to the structure
Array of structures Derived Types Create the array as done for normal array of integers E.g. STUDENT stuary[50]; To access the midterm marks of one of the subject 1 for student 4, we can write studAry[4].midterm[1]; The index operator the member operator, the selection operator have same precedence and associativity is from left to right
Structures and functions Derived Types 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 Individual members can be passed to the function Whole structure can be passed and the function can access the member using pass by value 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
Passing structures through pointers Derived Types 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 operator Since the address and member operator are the same level, we need to use the parenthesis only around the pointer dereference
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 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