Professional Documents
Culture Documents
06/04/06
Introduction
Structures are collection of related variables sometime referred to as aggregates under one name. Structures may contain variables of many different types in contrast to arrays that contain only elements of the same data type. Structures are commonly used to define records to be stored in files. Pointers and structures facilitate the formation of more complex data structures such as linked lists, queues, stacks and trees.
06/04/06 2
Structure definitions
Structures are derived data types they are constructed using objects of other types. Structure definition:
struct student{ int roll_no; char name[64]; float marks; };
keyword struct introduces the structure definition. The identifier student is the structure tag. struct student s1, s2; In the above declaration s1 and s2 are data items of type struct student.
06/04/06 3
Structures
struct student{
int roll_no; char name[64]; float marks; };
Members of the same structure must have unique names. Each structure definition must end with a semicolon.
06/04/06
Declarations
struct student{ int roll_no; char name[64]; float marks; }; /*this is structure definition */ struct student s1, s2[4], *ptr; /*declaration of variables of type struct student */
struct student is a user defined type and can be used to declare as many variables of that type as needed.
06/04/06 5
Declarations
struct { int roll_no; char name[64]; float marks; } s1, s2[4], *ptr;
In the above one declaration is done along with definition. Tag name can be omitted if the declarations are done along with definition. In the above case, variables cannot be declared separately, since tag name is missing.
06/04/06 6
Intialization
Like array intialization a structure canbe initialized with a list of constant expressions, as in: struct student s1 = {123456, ram, 24.5};
06/04/06
s1.roll_no = 123456; strcpy(s1.name, Ram); s2[0].roll_no = 234123; s2[0].marks = -1.0; ptr = &s2[1]; ptr ->roll_no = 987654;
06/04/06 8
Precedence
1) 2) 3) 4) 5)
06/04/06
struct student s1, s2, *ptr; if (s1 == s2) { . } /* not allowed */ ptr -> roll_no (*ptr).roll_no Note, parentheses are a must in the above!
06/04/06
10
Nested structures
Structures can be nested
struct point { double x; double y; }; struct rect { struct point p1, p2; }; struct rect screen; screen.p1.x = 0.0; /* OK */
06/04/06 11
Nested structures
Is the following OK?
struct nes{ double x; struct nes p; }; No, this is not allowed ! How much space this structure variable should be allotted is a problem.
06/04/06
12
Nested structures
But the following is alright !!
struct list { double item; struct list *next; }; Size calculation is not a problem. Indeed, for linked lists, trees, etc we require structures like this. We will see some simple linked lists soon.
06/04/06 13
Pointers to structures
Passing a large structure to a function takes large computational resources (space, time). Instead one can pass just addresses (as we did for arrays). This is faster. Does not require to create a local copy for the entire structure. But, one should be careful; now the function can access the original variable. Const pointers is a good remedy for these problems.
06/04/06 17
06/04/06
18
typedef
The keyword typedef provides a mechanism for creating synonyms (or aliases) for previously defined data types. typedef struct student Stud; Stud s1; /* s1 is variable of type struct student */ Stud is an alias for struct student
06/04/06
20
06/04/06
21
typedef
typedef can improve readability. Even for basic data types one can create alias names. typedef int Integer;
06/04/06
22