Structure and Unions.docx
Document Details
Uploaded by Deleted User
Tags
Full Transcript
**[Structure and Unions --unit 4]** Arrays provide a means to homogenous data items into a single named unit.But most of the applications require the grouping together of heterogeneous data items. Such an entity is called **structure** and the related data items use in it are referred to as members...
**[Structure and Unions --unit 4]** Arrays provide a means to homogenous data items into a single named unit.But most of the applications require the grouping together of heterogeneous data items. Such an entity is called **structure** and the related data items use in it are referred to as members. Thus a single structure might contain integer elements, floating point elements and character elements. Pointers, arrays and other structures can also be included as elements within a structure. C supports constructed data type known as structures, a mechanism for packing data of different types as a single entity. **[DEFINING A STRUCTURE]** Every structure must be defined before it appears in a C program.The general form of structure definition is as follows, struct tagname { data-type member\_1 ; data-type member\_2; \-\-\-\-\-\-\-\-\-\-- \-\-\-\-\-\-\-\-\-\-- data-type member\_n; } ; Here **struct** is a required keyword ,tagname is a name of the structure and member\_1 ,member\_2, member\_n are individual member declarations. The individual members can be ordinary variables, pointers, arrays or other structures. The member names with in a particular structure must be distinct from one another though a member name can be same as the name of a variable defined outside of the structure. Example struct student { int Regno; char Name\[20); int m1; int m; int m3; int Total\_marks; float Avg; }; This structure named as student (i.e, the tag is student) contains seven members, an integer variable Regno ,a 20-element character array (Name \[20\]). integer variables m1,m2,m3,Total\_marks and a floating type variable Avg. **[Declaring Structure Variables:]** After defining a structure format we can declare variables of that type. It includes following elements: - Keyword struct - The structure tage name - List of variables separated by commas - Terminating semicolon **struct structure-type variable- list;** example: struct student s1,s2,s3; now our structure student has 7 members (Regno,Name...) and 3 variables s1,s2 and s3. struct student { int Regno; char Name\[20); int m1; int m; int m3; int Total\_marks; float Avg; }; struct student s1,s2,s3; **[Accessing structure members:]** We can access and assign values to the members of a structure in a number of ways. The regno has no meaning. Whereas s1.regno has a meaning.Therefore the member operator.(dot operator) is establishes link between a member and a variable. We can assign values to the members of s1: s1.Regno=101; strcpy(s1.Name,"Anil"); s1.m1=90;...etc we can also use scanf() to give values through keyboard. scanf("%d",&s1.Regno); **[Structure Initialization:]** Like any other variables structure variable can be initialized at compile time. struct student { int Regno; char Name\[20); int m1; int m; int m3; }; struct student s1 ={101,"anil",90,78,85 }; **[Copying and Comparison of Structure Variables:]** Two variables of the same structure type can be copied the same way as ordinary variables. If s1 and s2 are belong to same structure then the following statements are valid. s2=s1; s1=s2; but s1==s2; s1!=s2 are not valid. Because c does not permit any logical operations on structure variables. **Memberwise comparison is allowed and it is used to decide whether 2 variables are identical.** struct student { int Regno; int m1; }; struct student s1 ={101 ,90 }; struct student s2 ={102 ,85 }; main() { s3=s2; x=((s3.Regno==s2.Regno)&&(s3.m1=s2.m1))?1:0; if(x==1) printf("s3 and s2 are same"); else printf("s3 and s2 are not same"); } **[Arrays of Structures]** We can declare an array of structures, each element of the array representing a structure variable. For example struct marks student\[100\]; This defines an array called student with 100 elements. Each element is defined to be the type of struct marks. **struct marks** **{** **int subject1;** **int subject2;** **int subject3;** **};** **main()** **{** **struct marks student\[3\]={{60,70,80},{45,78,90},{78,90,99}};** This declares the student as array of 3 elements student\[0\],student\[1\],student\[2\] and initializes their members as follows. student\[0\].subject1=60; student\[0\].subject2=70........so on **[Arrays within structures:]** C permits the use of arrays as structure members. We can use int/float type of arrays. Arrays can be one dimensional or multidimensional. struct marks { int rno; float subject\[3\]; } student\[2\]; Here subject contains 3 elements. Subject\[0\],subject\[1\] and subject\[2\]. And these can be accessed using proper subscript as follows student\[0\].subject\[1\]...... **[Structures within structures:]** Structures within a structure means nesting of structures. Nesting is permitted in C. Consider the following. Struct salary { char name; char department; int basic\_salary; int DA; int HRA; int special\_allowance; } employee; We can group the above allowance items together and declare them under a substructure as follows: **Struct salary /\* nesting\*/** **{** **char name;** **char department;** **struct** **{** **int basic\_salary;** **int DA;** **int HRA;** **int special\_allowance;** **}** **allowance;** **}** **employee;** In the above salary structure contains a member named allowance, which itself is a structure. We can access members as follows:- employee.allowance.DA; employee.allowance.HRA; **[Structures and functions:]** C allows the passing of structure values as arguments to functions. There are 3 methods. 1. Pass each member of the structure as an actual argument of the function call. But if the structure size is large, it becomes unmanageable. 2. Passing the copy of the entire structure to the called function.Any changes made to structure member within the function are not reflected in the original structure. 3. This method employs a concept caleed pointers to pass the structure as an argument