
Structures and Unions in Programming
Explore the concept of structures in programming, representing collections of data items, and learn how to define, declare, and access structure variables efficiently. Gain insights into the powerful tool of structures for managing logically related data items in program design.
Download Presentation

Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
Presentation Transcript
Introduction 2 ARRAYS represent a group of data items that belong to the same type, such as int or float Structure is a constructed data type to represent a collection of data items of different types using a single name A structure is a convenient tool for handling a group of logically related data items It is a powerful concept that we may often need to use in our program design EEE1002
Defining a Structure 3 Structures must be defined first for their format that may be used later to declare structure variables Structure tag (name of the structure) Keyword structure elements (members) Structure variables (structure type book_bank ) EEE1002
Defining a Structure 4 It simply describes a format called template to represent information as shown below : EEE1002
Declaring Structure Variables 5 After defining a structure format we can declare variables of that type A structure variable declaration is similar to the declaration of variables of any other data types It includes the following elements : 1. The keyword struct 2. The structure tag name 3. List of variable names separated by commas 4. A termination semicolon ; EEE1002
Declaring Structure Variables 6 For example, the statement book1, book2, and book3 : variables of type struct book_bank The members of a structure themselves are not variables They do not occupy any memory until they are associated with the structure variables such as book1 EEE1002
Declaring Structure Variables 7 Other valid declaration #define main() main () The use of tag name is optional EEE1002
Declaring Structure Variables 8 EEE1002
Accessing Structure Members 9 We can access and assign values to the members of a structure in a number of ways The members should be linked to the structure variables in order to make them meaningful members The link between a member and a variable is established using the member operator . known as dot operator or period operator is the variable representing the price of book1 and can be treated like any other ordinary variable (structure variable).member : variable EEE1002
Accessing Structure Members 10 Example) Complex arithmetic structure complex { double real; double imag; }; structure complex add(struct complex x, struct complex y) { struct complex z; z.real = x.real + y.real; z.imag = x.imag + y.imag; }; x.real : variable of type double EEE1002
Accessing Structure Members 11 EEE1002
Structure Initialization 12 Like any other data type, a structure variable can be initialized at compile time EEE1002
Structure Initialization 13 A lot of variation is possible in initializing a structure The following statements initialize two structure variables EEE1002
Structure Initialization 14 When some members are not initialized If a structure variable has static storage, its members are implicitly initialized to zero of the appropriate type. If a structure variable has automatic storage, its members have no default initialization. If a structure variable is partially initialized, all the uninitialized structure members are implicitly initialized to zero no matter what the storage class of the structure variable is. EEE1002
Structure Initialization 15 struct one { int a; int b; int c; }; void main() { struct one z1; // Members in z1 do not have default initial values. static struct one z2; // z2.a=0, z2.b=0, and z2.c=0. struct one z3 = {1}; // z3.a=1, z3.b=0, and z3.c=0. } EEE1002
Copying and Comparing Structure Variables 16 Two variables of the same structure type can be copied the same way as ordinary variables struct personal person1, person2; If person1 and person2 belong to the same structure, then the following statements are valid : However, the following statements are not permitted : C does not permit any logical operations on structure variables In case, we need to compare them, we may do so by comparing members individually EEE1002
Copying and Comparing Structure Variables 17 EEE1002
Operation on Individual Members 18 The individual members are identified using the member operator, the dot A member with the dot operator along with its structure variable can be treated like any other variable name float sum = student1.marks + student2.marks; student2.marks *=0.5; We can also apply increment and decrement operators to numeric type members student1.number ++; ++ student1.number; EEE1002
Arrays of Structures 19 We may declare an array of structures, each element of the array representing a structure variable Array of a structure int string Int array float student[0], student[1], student[2] : structure of type marks and initializes their members student[0].subject1 = 45; student[0].subject2 = 68; . . . . . EEE1002
Arrays of Structures 20 EEE1002
Arrays of Structures 21 EEE1002
Arrays Within Structures 22 C permits the use of arrays as structure members For example, the following structure declaration is valid : Here, the member subject contains three elements, subject[0], subject[1] and subject[2] These elements can be accessed using appropriate subscripts EEE1002
Arrays Within Structures 23 EEE1002
Arrays Within Structures 24 EEE1002
Structures Within Structures 25 nestingof structures The salary structure contains a member named allowance, which itself is a structure with three members EEE1002
Structures Within Structures 26 An inner structure can have more than one variable The inner structure has two variable, allowance and arrears This implies that both of them have the same structure template EEE1002
Structures Within Structures 27 We can also use tag names to define inner structures pay template is defined outside the salary template and is used to define the structure of allowance and arrears inside the salary structure EEE1002
Structures and Functions 28 It is natural that C supports the passing of structure value as arguments to functions The general format of sending a copy of a structure to the called function is : The called function takes the following form : EEE1002
Structures and Functions 29 The following points are important to note : The called function must be declared for its type, appropriate to the data 1. type it is expected to return The structure variable used as the actual argument and the corresponding 2. formal argument in the called function must be of the same struct type The return statement is necessary only when the function is returning some 3. data back to the calling function When a function returns a structure, it must be assigned to a structure of 4. identical type in the calling function The called functions must be declared in the calling function appropriately 5. EEE1002
Structures and Functions 30 EEE1002
Structures and Functions 31 EEE1002
Unions 32 Unions follow the same syntax as structures However, there is major distinction between them in terms of storage In structures, each member has its own storage location, In unions, all the members of a union use the same location This implies that, a union may contain many members of different types, but it can handle only one member at a time EEE1002
Unions 33 This declares a variable code of type union item The union contains three members, each with a different data type However, we can use only one of them at a time. This is due to the fact that only one location is allocated for a union variable, irrespective of its size EEE1002
Unions 34 The compiler allocates a piece of storage that is large enough to hold the largest variable type in the union In the declaration above, the member x requires 4 bytes which is the largest among the members Figure 10.7 shows how all the three variables share the same address This assumes that a float variable requires 4 bytes of storage EEE1002
Unions 35 To access a union member, we can use the same syntax that we use for structure members Unions may be used in all places where a structure is allowed The notation for accessing a union member which is nested inside a structure remains the same as for the nested structures Union may be initialized when the variable is declared EEE1002
Unions 36 union Data { }; int main( ) { int i; float f; char str[20]; data.i : 1917853763 data.f : 122360580327794860452759994368.000000 data.str : C Programming union Data data; data.i = 10; data.f = 220.5; strcpy( data.str, "C Programming"); printf( "data.i : %d\n", data.i); printf( "data.f : %f\n", data.f); printf( "data.str : %s\n", data.str); } EEE1002
Unions 37 int main( ) { } union Data data; data.i = 10; printf( "data.i : %d\n", data.i); data.f = 220.5; \ printf( "data.f : %f\n", data.f); strcpy( data.str, "C Programming"); printf( "data.str : %s\n", data.str); data.i : 10 data.f : 220.500000 data.str : C Programming EEE1002
Size of Structures 38 We normally use structures, union, and arrays to create variables of large sizes We may use the unary operator sizeof to tell us the size of a structure (or any variable) The expression sizeof (struct x) will evaluate the number of bytes required to hold all the members of the structure x EEE1002
Case Studies 39 EEE1002
Case Studies 40 EEE1002
Case Studies 41 EEE1002
Case Studies 42 EEE1002