Transcript Document

derived types
•
•
•
•
•
•
The type definition
Enumerated types
Accessing structures
Complex structures
Arrays of structures
Structures and functions
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
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 upper case
Traditionally
or derived type
typedef standard
char *STRING;
Keyword
STRING stringPtrArray[20];
o
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
Derived
used
Types
• Keep enumerated types separate from integer types
Enumerated 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
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;
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
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”);
}
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
Example
typedef struct
{
int x;
int y;
float t;
char u;
} SAMPLE;
SAMPLE sam1;
SAMPLE *ptr;
ptr=&sam1;
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 structure
o
o
o
o
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
Derived
Types
Structure operations
• A structure can only be copied to another structure of the same
type using the assignment operator
Derived
Types
Pointers to structures
• Define a pointer for the structure
o
o
o
o
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
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
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
Derived
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 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
Derived
Types
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 studAry[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
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
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
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.
&ptr numerator
&(*pFr).denominator // member operator has higher precedence over Derived
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
• 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
Derived
Types