 Welcome back, in the previous segment we discussed how to create structure types and also create variables or structures or instances of an already created structure type. In this segment we are going to see what kinds of operations we can perform on structures. So the first operation or the first thing we can do with structures is to initialize them during creation itself. So let me just remind you, this is our structure type book, it contains members, title and price and I can create an instance or I can create a structure of this type and I can initialize it. So the initialization as usual happens through braces and the first element in the braces initializes the first member which is title and so b.title would be set to the string on education. Then the second member in the braces initializes the second element in the braces I should perhaps say initializes the second member of the structure type book which is price. So this initializes b.price to 399. So again let me remind you that on education is a character string and it is stored with a terminating null as usual. And you might have lots of members in a particular structure type so you have to give as many values in braces and having the appropriate type. You can make structures unmodifiable by adding the keyword const. So for example you may define a structure variable c with title member being the outsider and the price being 250 but here you are saying that c cannot be modified either of the two members of c cannot be changed as your program executes. Now one structure can contain another so for example we might have a structure point which contains say the coordinates of the point and now if I want to define a structure disk it would be natural to have the center being defined as a point. So the first member here is a point which is a type so all that we really need is a proper type and so point center is perfectly fine. And then the second member is of type double and we are going to call it radius. So we can create a structure d or an instance of type disk by writing disk d. So I can write d dot radius because after all radius is a member of d and I can say d dot radius to be 10 but d has a member center which in turn has a member x. So I can write d dot center dot x equals 15 and of course I can write d dot center dot y as well to whatever y coordinate I think the center ought to have. I can assign one structure to another. So basically all members of the right hand side get copied into the corresponding members of the left hand side. So the name of the structure stands for the entire collection unlike array names. So array names stand for the address where the array has been allocated memory. Structures are not like that. The structure name stands for the entire collection and as I said earlier a structure is a variable or you might actually think of it as a super variable because it contains you can also think of the members being the variables which are contained in this bigger variable. So as an example we have a book B with title on education and price 399 and say we have a book C. So now I can write C equal to B. This will copy both the members and so if I print C dot price the price member of C which is which has been copied over from B is 399 and so 399 will get printed. This can be used with functions and they can be passed to functions by value. So when you pass it by value it is sort of like assignment all the members are copied to the parameter structure and of course the usual thing is that the types must match. So even in an assignment and even in passing the types must match. Structures can also be passed by reference and this means exactly the same thing as in case of variables. So in the called program the parameter name refers to the same variable which was passed from the calling program, the calling function and you can return structures as well. So what does this mean? So all the data members of the structure that you want to return are copied back to a temporary structure in the call in the calling program and then you can do whatever you want with that temporary structure. See this temporary structure is going to be in place of the call, so the call should be thought of as returning the result and that is where the temporary structure is going to be. So we will see examples. So first an example of passing by value. So here is a structure point the one that we saw earlier it has members x and y both double. Now here is a function called midpoint. So it takes as arguments a and b which are both points and it does it creates a new point mp and returns that. So we will see exactly what happens. And then there is the main program and the main program is going to call this function midpoint. So the first call the red call suppose we start executing main and come to the red call. So what happens? So the arguments p and q are copied to the parameters a and b. So this is the call and these arguments pq are copied to these parameters a and b and now the code of midpoint starts executing. So the first step is that a local structure mp of type point is created and this is created in the activation frame of this function midpoint. Its x and y variables are set suitably. So they are set to the mean of the x and y variables of x and y coordinates of points a and b. And then this mp is returned. So this happens as follows. So a temporary structure of type point is created and that structure sort of stands in for this call and the elements of the members of mp are copied into that temporary structure. So or you can think of this entire structure is copied into that entire structure. And subsequently what will happen main program will start executing again and this will be assigned to r. So mp is copied into the temporary structure and the temporary structure is copied into structure r. And then in this we can print r.x that is perfectly acceptable and here is another call. So here we are calling midpoint but we are directly taking its x coordinate. We are not putting it into any local variable and taking the coordinate, taking the member. We can do this. We can just put dot x and this will just mean whatever this call returns take its x member. We can pass by reference as well and in this case the code is really the same except that we have these two ands which indicate that a and b are passed by reference. So in the execution of midpoint pq the parameters a, b will refer to variables b and q nothing will be copied over. Now there is no copying of pq and this will save execution time if the structures being passed are large. And indeed if you are passing large structures it is a good idea to pass them by reference. The rest of the execution is as before and normally if I have a reference parameter I have that because I want to modify it and therefore it is expected that the reference parameters should be variables. So, these, so whatever is being called over here should be variables because only then can I modify them, modify these reference parameters in the code. However, this const says that this code promises not to modify these things, these parameters and therefore with this const you can pass constant points as arguments to midpoint as well. So constant structures can be passed as arguments. So in fact I can write midpoint of the midpoint of p and q and q. So this will sort of get me a point which is a quarter of the distance away towards q between p and q. So I can do that as well, I can nest these calls also. Next I can have arrays of structures if I wish. So for example I can write disk D and this just defines variables D0 through D9 each of which is a disk. Similarly I can have a variable lib maybe short for library which is an array of 100 books. So again this defines variables lib0 through lib99 each of which is a structure of type book. So now these variables are just like ordinary disk variables and so I can take their center x and all the usual stuff. And this also is sort of a usual variable so I can take its member but the member happens to be an array so I can write, I can index into it and so this is going to print the third character of the fifth book in the array library or I guess I should have called it lib because this is how we created it so it is not really read this as lib in both places. So what have we discussed? We have discussed a number of operations on structures, initialization, nesting and by that I mean having a member be another structure and we have discussed passing and returning structures from functions we have also discussed creating arrays of functions. In the next segment we will have a detailed example involving structures but let us take a quick break.