Sie sind auf Seite 1von 23

Structures in C

CI011-JIET

5/20/2012

Babita Tiwari

Data Structures (struct)


Arrays require that all elements be of the same data type. Many times it is necessary to group information of different data types. An example is a materials list for a product. The list typically includes a name for each item, a part number, dimensions, weight, and cost. C and C++ support data structures that can store combinations of character, integer floating point and enumerated type data. They are called a structs.
5/20/2012 Babita Tiwari 2

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

Data with structures


Clubbed or grouped together. Treated as a complete entity with different type of values clubbed together. Easy to read and establish relation. Complex Hierarchies can be implemented using nested structures.
5/20/2012 Babita Tiwari 4

Data with structures


Movie_Details String Integer Float Movie_Name Year_of_Release Ticket_Cost

5/20/2012

Babita Tiwari

Declaring structures syntax


struct
{ <Optional Tag name>

<data type> <data type> <data type> } <object name> ;

<variable name> ; <variable name> ; <variable name> ;

5/20/2012

Babita Tiwari

Does Not Reserve Space

Declaring Structures (struct)

Reserves Space
struct my_example { int label; char letter; char name[20];
} mystruct ;

struct my_example { int label; char letter; char name[20]; };


/* The name "my_example" is

called a structure tag


5/20/2012

*/

Babita Tiwari

Declaring structures example


struct
{ <Optional Tag name> Movie_Details Movie_Name[20] ; <variable name> <variable name> Year_of_Release ; <variable name> ; Ticket_Cost char <data type> <data type> int <data type> float } <object name> ; God_Father

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

Alias for structures


With typedef we can create an alias for a declared structure e.g. :
typedef struct { int x; int y; } point; point init, last; /* declare two structures of point type*/

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

left is the variable of struct dclared before*/

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() {

/*both rose.name and rose.family are treated as simple string variables*/

}
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

structure point type */

5/20/2012

Babita Tiwari

15

Structs with Union


The union uses the same memory location for either rank or a character string (deg) depending on the answer to the prompt for student status in main( )

5/20/2012

Babita Tiwari

16

Structs with Union (cont.)


#include <stdio.h> union status { int rank ; char deg[4] ; }; struct personal { long id ; float gpa ; union status level ; }; struct identity { char name[30] ; struct personal student ; };

5/20/2012

Babita Tiwari

17

Structs with Union (cont.)


int main( ) { struct identity jb ; char u_g; jb.student.id = 123456789 ; jb.student.gpa = 3.4 ; printf ("Enter student status - u or g\n"); scanf ("%c", &u_g); if (u_g == 'u') { printf ("Enter rank -- 1, 2, 3, 4 or 5\n"); scanf ("%d", &jb.student.level.rank); printf ("%s is level %d\n , jb.name , jb.student.level.rank); } /* End of if statement */

5/20/2012

Babita Tiwari

18

Structs with Union (cont.)


else { printf ("Enter degree sought -- ms or phd\n"); scanf ("%s", &jb.student.level.deg); printf ("%s is a %s candidate\n, jb.name , jb.student.level.deg ); } /* End of else statement */ printf ("%s %ld %f\n , jb.name , jb.student.id , jb.student.gpa ); printf ("%s%ld %f\n , ptr->name , ptr->student.id , ptr->student.gpa ); } /* End of program */

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

Das könnte Ihnen auch gefallen