 Dear Learner, myself Tawashi Kashyapdas, Assistant Professor in Computer Science from Hirano-Chandrabuya School of Science and Technology KKH state of mind University. Today I will discuss the concept of structures in C programming language, which is from the course Computer Programming Using C. This topic is included in various undergraduate and postgraduate as well as diploma programs of our University. We have already learned how to store X's elements in an array. All the information stored in an array belongs to the same data type. A structure extends the concept of arrays by storing related information of heterogeneous data types together under a single umbrella. Today we will see how a structure is defined, declared and accessed using C language. Now what is structure? A structure is similar to records. It stores related information about an entity. Structure is a user-defined heterogeneous data type. It consists of elements of different data types. With the use of structures, programmers can conventionally handle a group of related data items of different data types. A structure is therefore a collection of variables under a single name. The variables within a structure are of different types and each has a name that is used to select it from the structure. Now, how to declare a structure? A structure is declared using the keyword struct followed by a structure name. All the variables of the structure are declared within the structure. The data types of all these variables within a structure can be of different types. A structure is generally declared with the following syntax. First, we have to give the keyword struct, then we have to give a structure name. Then within second bracket, we have to give the structure element along with their data types. Here I have given two examples. One is for book entity and another is for customer entity. For book structure, we have declared five elements, three are character. First one is for storing the title of the book. Second is for storing the author name. Third is for storing the publisher name. Fourth is for storing the number of pages and fifth is for storing the price of the book. This is one book structure. Another is for customer entity. For this structure, first one integer variable is taken for storing the customer entity. Then for storing the name, character erased is declared. For storing address, another character is declared. And for storing the phone numbers, one long integer is declared. And for storing the date of birth, one character variable is declared. Now, how to declare structure variables? Structure declaration acts as a template which conveys structure information and member names to the compiler. A structure definition is usually written before main function. In such cases, the structure becomes global and all the functions can access the structure. Now, let us discuss how to declare structure variables. We can declare structure variables using the structure name, that is the tag name, anywhere in the program. For example, for the book structure, the statement, struckbook, book1, book2, book3, declare three structure variables, book1, book2 and book3 of type, struckbook. Let us consider another structure declaration, employee. For the employee structure, for storing the first name and last name, two character arrays are declared. Then for storing identity number, month, day and year, integer variables are declared. After declaring the structure, one employee variable, EMP1 is declared. Here we have declared a variable EMP1 which has six fields, some of them are integer and some are strings or character array as we have already mentioned. Right after the declaration, a portion of the main memory is reserved for the variable EMP1. Here the structure variable EMP1 takes the size of 38 bytes for different members of the employee structure. 15 bytes for first name, 15 bytes for last name, 2 bytes for storing the ID number, 2 bytes for storing month, 2 bytes for storing day and 2 bytes for storing the year. Now let me discuss how memory is allocated for a structure variable. Here a diagrammatic representation of memory allocation is shown. Here the book structure is again considered. For storing the title of the book, 20 bytes is reserved. For storing the name of the author, 15 bytes is reserved. For storing the name of the publisher, 25 bytes are reserved. And for storing pages, 2 bytes are reserved. And for storing the price of the book, 4 bytes are reserved. Here three structure variables are created. Book 1, Book 2, Book 3 and for each of the structure variables, this amount of memory will be reserved. And this is a diagrammatic representation of memory allocation for a structure variable. Now how to initialize a structure? Initialization of structure means assigning some constants or values to the members of the structure. A structure can be initialized in the same way as other data types. The value against the structure variable are enclosed in braces, that means within second brackets, and are separated by commas. Here a customer structure is considered. One customer variable C1 is declared by the statement struct customer C1. And some initial values are initialized in the structure variable C1. For customer ID, 01 is entered. For name of the customer, Rajiv Sharma is entered. And for address, one value is entered. And for phone number, phone number is entered and data bar is entered. And like this, a structure variable can be initialized. When all the members of a structure are not initialized, it is called partial initialization. In case of partial initialization, first few members of the structure are initialized and those that are not initialized are assigned default values. Here the book structure is again considered. And in the statement, struct book book 1 is equal to Java and y-connector is mentioned. That means for the title, Java is entered and for the author name, y-connector is entered. And the values for the three other structure elements are not mentioned. So the default values are by default entered in the memory location. For character, null value is entered. And for integer, 0 is entered. And for floating point value, 0.0 is entered. Now, how to access the members of a structure? A structure member variable is generally accessed using a dot operator. Syntax for accessing the members of a structure is, first we have to give the structure variable, then the auto operator, then the member name. Here again the customer structure is considered. Here in the example, c1.customerId is equal to 01. This statement means 01 is assigned to the customer ID of customer c1. Similarly, for storing the phone number of customer 1, one statement is written c1.phoneNumber equal to 943503322. That means this phone number is assigned to the phone number of customer 1. Thus we can initialize members of a structure variable. For inputting values of data members of structure variable, we may write scanf%d, m%book1.title scanf%d, m%book1.press. With this statement, we can enter the title and the price of book 1 through keyboard. Similarly, for displaying values of structure variable, we may write printf%d, book1.title printf%d, book1.press. With this statement, we can display the title and price of book 1 of the structure variable, book1. Now let us discuss area of structure. In programming, we may often need to handle many records. For example, in a class, there may be many numbers of students, say 50. So to keep the records of 50 students, we need an area of structure. Here one structure student is declared. This structure has four members. First one is integer for storing the roll numbers. Then for storing the name of the student, one character area is declared of size 20. Then for storing the course name, one character area is declared of size 15. And for storing the fees, one float variable is declared. After that, one area of structure is declared with the statement structstudent as 50. That means s is an area of structure of 50 students, each of which is of type structstudent. Thus, we can store information of 50 students in this variable s. Now let me show one computer program which is used to store information of numbers of students in an area of structure. This program is written in a diversity compiler. Here, first one structure is declared. The name is student. After that, with the statement structstudent as 20, that means this is an area of structure of 20 students. That means information of 20 students can be stored in this variable student. This is an area of structure. After that, one printf statement and one scanf statement is written. Printf statement for entering the number of students. And the scanf statement is used for entering the actual number of students. Suppose I want to store the number of information of 10 students. Here, two for loop is written. First for loop is used for entering the information for various students. And second for loop is used for displaying the information of students one by one. Here, output of the previous program is shown. Information of two students is shown here. If we enter information of more students, then information of all students will be shown in the display screen. Now I am going to discuss pointer to structure. We have already learned the concept of pointers. Pointer is a variable which holds the address of another variable. Instead of passing a copy of the whole structure to a function, we can pass only the address of the structure in the memory to the function. Then the program will get access to every member in the function. This can be achieved by creating a pointer to the address of a structure using the indirection operator. To write a program that can create and use pointer to structure, first let us define a structure. Here I have created a structure for the item entity. There are only two elements, structure elements, item code and price. Now let us declare a pointer to structure type item. And it can be declared with a statement, struct item pointer ptr. Because a pointer needs a memory address to point to, we must declare an instance of type item. For this, struct item p is declared. A demonstration of pointer to structure is given in this program too. Here item structure is considered and one pointer to structure is declared by the statement struct item pointer ptr. Then address of i is assigned to ptr by the statement ptr equal to ampersand i. Then 20 is inserted to the element code and 350 amount is inserted in the element price of, which is pointed by the pointer ptr. After that it is displayed by two printf statement. One is for displaying the item code and another is for displaying the price. Like this we can use the pointer to structure. And output of this program is given. Item code is 20 and price is 350. Today I have discussed how to declare a structure, how to define a structure, how to create a structure variable, how to initialize a structure, array of structure and pointer to structure. In my next class I will discuss the concept of union. Thank you.