Beruflich Dokumente
Kultur Dokumente
CI011-JIET
5/20/2012
Babita Tiwari
A struct is a User defined data type composed of members that are each fundamental or derived data types. A single struct would store the data for one object. An array of structs would store the data for several objects. A struct can be defined in several ways .
Structures (struct)
5/20/2012
Babita Tiwari
5/20/2012
Babita Tiwari
5/20/2012
Babita Tiwari
Reserves Space
struct my_example { int label; char letter; char name[20];
} mystruct ;
*/
Babita Tiwari
Here God_Father is a variable of type Movie_Details having Movie_Name, Year_of_Release and Ticket_Cost as their member data. Every time we declare a variable of Movie_Details type a new structure is created with the data members (in green).
5/20/2012 Babita Tiwari 8
Declaring structures
struct data type is a user-defined data type. The structure tag provides a short hand for declaring more structure of a specific type. E.g. struct Movie_Details DDLJ;
/*Here DDLJ is a new structure of type Movie_Details declared earlier*/
5/20/2012 Babita Tiwari 9
Declaring Structures
Using typedef
The keyword typedef creates a synonym or alias for a data type. Example: typedef unsigned long ulong;
an alias for unsigned long */ ulong width, height; /* creates variables of type ulong that is unsigned long */
/* ulong is now
5/20/2012
Babita Tiwari
10
5/20/2012
Babita Tiwari
11
Accessing Strucures
The individual members of a structure can be accessed using ".", the member access operator.
E.g. for the following structure :
typedef struct ABC { int x; int y; } point; point left;/*point is the alias
void main() { left.x=0; left.y=0; /*left.x is the x member of structure left*/ printf(Left axis : %d, %d, left.x, left.y); }
5/20/2012 Babita Tiwari 12
Accessing Strucures
Syntax : <structure variable name>.<member variable name> When a data member of a structure is accessed the operations performed on it would be same as are performed for any individual variable of its type.
5/20/2012
Babita Tiwari
13
Example program
struct plant { char family[30]; char name[20]; float value; } rose; void main() {
}
5/20/2012
strcpy(rose.family, Rose); strcpy(rose.name,Black Rose); printf(The family is : %s, rose.family); printf(The name is : %s, rose.name);
Babita Tiwari 14
Arrays in structures
When we need a collection of clubbed data then we use array of a structure. For example :
struct point{ int x; int y; }listOfPoints[20]; /*listOfPoints is an array of
5/20/2012
Babita Tiwari
15
5/20/2012
Babita Tiwari
16
5/20/2012
Babita Tiwari
17
5/20/2012
Babita Tiwari
18
5/20/2012
Babita Tiwari
19
Enumeration
Enumeration is a user-defined data type. It is defined using the keyword enum and the syntax is: enum tag_name {name_0, , name_n} ; The tag_name is not used directly. The names in the braces are symbolic constants that take on integer values from zero through n. As an example, the statement: enum colors { red, yellow, green } ; creates three constants. red is assigned the value 0, yellow is assigned 1 and green is assigned 2.
5/20/2012
Babita Tiwari
20
Enumeration
/* This program uses enumerated data types to access the elements of an array */ #include <stdio.h> int main( ) { int March[5][7]={{0,0,1,2,3,4,5},{6,7,8,9,10,11,12}, {13,14,15,16,17,18,19},{20,21,22,23,24,25,26}, {27,28,29,30,31,0,0}}; enum days {Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday};
5/20/2012
Babita Tiwari
21
Enumeration(Continue)
enum week {week_one, week_two, week_three, week_four, week_five}; printf ("Monday the third week " "of March is March %d\n", March [week_three] [Monday] ); }
5/20/2012
Babita Tiwari
22
output
Monday the third week of March is March 14
5/20/2012
Babita Tiwari
23