 Hello and welcome to the NPTEL course on an Introduction to Programming through C++. I am Abhiram Ranade and in today's lecture sequence I am going to be talking about structures. So let us take a look at where we are in the course. So you have really learned enough to write essentially any program. So you have learned basic control statements, you have learned functions, you have learned arrays and in this lecture we are going to learn language features that will make writing programs more convenient, safer and modular. These terms may not mean anything to you right now but they will become clearer as we proceed. So let me begin with a difficulty. So if you are writing a large program then it will have lots of variables and just managing all the variables is often a bit tiring. It is like you have lots of papers strewn over the table and you do not know how many papers you have which is what you called which variable, all such things and on a table we can bring some neatness by putting related papers into files. So that way we have groups and we know what we can sort of roughly know what is in which group and that helps us figure out what all we have. So an interesting question is could we do something like that with variables and that in fact is the solution that structures gives us and that is the main topic of this lecture. So here are the high level ideas. Most entities we deal with in programming have lots of attributes. If your program is about simulating movement of stars then each star has a position, capacity, mass, if our program is about managing books in a library then each book has an author, library number, who has borrowed it of course the title and the key idea in structures is to collect together all information about an entity into a group or you might even think of it as a super variable. And a super variable is called a structure. So here is the outline for this lecture. So we are going to start by talking about structures. So we will talk about the basic facilities that C++ provides to define structures and by the way the structures are inherited from the C language. We will also talk about operations that can be performed on structures and also lots of examples. So a very high level overview, a structure is nothing but a collection of variables and the term member is also used. The members of a structure are the variables in the collection. So a structure can be thought of as a super variable and it denotes all the memory used for all the members. Each structure has a name, the name refers to the super variable or in other words the name refers to the region of memory in which all the members are present. So it refers to the entire collection. Each structure also has a type and the type defines what variables there will be in the collection. So let us talk about structure types first. You can define a structure type for each type of entity that you want to represent on the computer and these types are what might be called programmer defined types. For example to represent books you can define a book structure type. When you define a structure type you must say what variables each structure of that type will contain. So continuing the example, if you define a structure or structure type to represent books you may wish to have variables to store the name of the book, its price and other attributes. So here is how you define a structure type. The general form is the keyword struct followed by the name of the structure type that you are defining. Then it consists of several variables or members. So for each member you first have to specify the type, the type of that member and then you have to give it a name and that is it. So it ends with a brace and usually braces do not have semicolons as we have seen them so far but here there is a semicolon. And here is an example of a specific structure type that we might define consistent with this general form. So here is a type for storing data about books. So we are going to call that structure type book capital B and then it will have members. One member is a title which is a character array. So members can be arrays or members can be just plain variables. And then there is another member which is price and it is of type double. You could have more but let us say we just have these two. So as I said earlier a structure type is a user defined data type and it is really similar to int, char, double which are the primitive data types. And the structure type and member names can be any identifiers. So how do you create a structure of a type that you have defined earlier? So we just defined a structure type book. So if you want to create a structure of that type we just simply write book PQ. So this creates two structures PQ of type book. Each created structure has all members defined in the structure type definition. So at this point we have actually allocated memory. When we defined a structure type we did not allocate memory. We just said that look here is sort of the format using which you can create structures and the structures actually cause variables to be created and memory to be allocated. Now member x of structure y can be accessed by writing y dot x. So since we have a structure P and on the last slide we said that structure type book has members title and price. We can get to the price member of P by writing P dot price. And this statement is storing 399 into that member. Now we can print it if you wish. So if we write C out P dot title or C out P dot price whatever the contents of these members or these member variables are will be printed. So P dot title is presumably the name of the book represented by the structure P and so that will get printed. Of course we will have to assign it first and so once it is assigned we can print it out in this manner. So what have we discussed? We have discussed how to create structure types and then we discussed creating variables and instances of an already created structure type. Next we are going to discuss operations on structures but let us take a quick break.