derived types The type definition Enumerated types Accessing

  • Slides: 21
Download presentation
derived types

derived types

 • • • The type definition Enumerated types Accessing structures Complex structures Arrays

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

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

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 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 * string. Ptr. Array[20]; can be written as upper case Traditionally or derived typedef standard char *STRING; Keyword STRING string. Ptr. Array[20]; o Derive d Types

Enumerated types • enum is built on integer types • Each integer value is

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 Derive d used Types • Keep enumerated types separate from integer types

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

Enumerated types • enum { enumeration constants } variable_identifier; o Format 1 : Enumerated variable o Format 2: enumerated tag • enum tag { enumeration constants }; • enum tag variable_identifier; • 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}; o o o Here jan equates to 0, feb to 1 and so on enum months {jan=1, feb , jun=6, aug=1}; enum months birthmonth; • It is list of one or more identifiers separated by commas Derive d Types

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

Enumerated type definition • enum colors{ red, white, blue}; • enum colors acolor; o Format 1 : Enumerated variable o Format 2: Enumerated typedef • typedef enum{ red, white, blue } COLORS; • COLORS a. Color; Derive d Types

example • • • • • /* program to demonstrate the use of enum

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”); } Derive d Types

Structure format variation struct { § … } variable_identifier; ----------------------------------------struct tag { § …

Structure format variation struct { § … } variable_identifier; ----------------------------------------struct tag { § … } variable_identifier; struct tag variable_identifier; --------------------------------------------typedef struct { § … } TYPE_ID; • TYPE_ID variable_identifier; Derive d Types

Example typedef struct { int x; int y; float t; char u; } SAMPLE;

Example typedef struct { int x; int y; float t; char u; } SAMPLE; SAMPLE sam 1; SAMPLE *ptr; ptr=&sam 1; Derive d Types

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

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 structure o o e. g. If (employee 1. gender = = ‘M’ ) employee 1. salary +=employee 1. hra; scanf(“%d %f %c “, &sam 1. empcode, &sam 1. salary, &sam 1. gender); (. ) precedence 17, postfix -16, unary increment -15 sam 1. x++ and ++sam 2. x are valid Derive d Types

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

Structure operations • A structure can only be copied to another structure of the same type using the assignment operator Derive d Types

Pointers to structures • Define a pointer for the structure SAMPLE *ptr; ptr=&sam 1;

Pointers to structures • Define a pointer for the structure SAMPLE *ptr; ptr=&sam 1; Structure can be accessed using indirection operator (*) refering to the whole structure o E. g. o o o § (*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 Derive d Types

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

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 Derive d Types

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

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 Derive d array element Types • When structure contains array we can use pointers to refer directly to the array elements

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

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 o char may[]=“May” stamp. date. month=may; // assigns month “May” to the structure Derive d Types

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

Array of structures • 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 o stud. Ary[4]. midterm[1]; o The index operator the member operator, the selection operator have same precedence and associativity is from left to right o o o Derive d Types

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

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 Individual members can be passed to the function Whole structure can be passed and the function can access the member using pass by value o 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) o o • We can send the individual elements or the whole structure to a function • A function can also return a structure Derive d Types

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

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. &ptr numerator Derive &(*p. Fr). denominator // member operator has higher precedence over d Types 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

unions

unions

 • A union is a construct that allows memory to be shared between

• 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 shared. Data{ • char ch. Ary[2]; • short num; }; • Both share the same location i. e ch. Ary[0] is MSB byte of num and ch. Ary[1] is LSB byte of num. • Member accessing is similar to structures. Derive d Types

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

• 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