 Hello and welcome. This is the first part of a two-part lecture on structures and pointers. Here is a quick recap of some of the relevant topics we have already studied. We have studied about structures as collections of variables, arrays and other structures. We have seen how to statically declare variables or arrays of structure types. We have also seen how to access members of structures. In earlier lectures on pointers, we had also studied about the organization of main memory, particularly about locations in memory and their addresses. And in our lectures on pointers, we had studied about pointers to variables as well as arrays of basic data types like integer, float, character and the like. In this lecture, we are going to study about pointers to variables of structure types. And we are also going to see how to access members of structures through pointers. So, let us quickly recall the organization of memory and addresses of memory locations. So, main memory can be thought of as a sequence of physical storage locations as is depicted here. Each of these locations stores one byte or 8 bits as is shown here. Each horizontal rectangle can be thought of as one location of memory storing 8 bits. And these 8 bits will also be called the content or value of that location. Each physical memory location is also identified by a unique address which can be thought of as the sequence, as the index of that memory location in the sequence of memory locations. So, for example, this particular location has an address 409 in hexadecimal. And the content of that memory location is all zeros. If you also recall from our earlier lectures, we had said that when a program executes or in other words, when a process runs, the operating system allocates a part of main memory for use by the process. So, if this is the main memory, the operating system might have allocated this part of it for use by the process. Now, this memory in turn is divided into three segments. The code segment which stores the executable instructions in the program, the data segment for dynamically allocated data and the stack segment which basically stores the call stack which has the activation records of all functions that are called in the program. And each activation record, if you recall, has memory allocated for all local variables of the corresponding function. So, let us quickly see how structures are going to be laid out in main memory. So, here is a simple program. This is the main function within which I have declared a structured data type, mystruct type. This has three members. z is of type character, members x and y are of type int. And then I have a variable p1 of structure type, mystruct type. And I also have a variable a of type int. Now, we know that every integer variable needs 4 bytes of storage. So, I can visualize the main memory. And the main memory here is divided into three segments, the code segment, data segment and stack segment, a being a local variable of the function main, memory for a will be allocated in the activation record for main when main is called by the operating system. And this activation record will reside in the call stack in the stack segment. So, therefore, memory for a is really allocated in the stack segment. And since we need 4 bytes and since each location of main memory stores just only 1 byte, we have 4 consecutive locations here allocated for the integer variable a. Now, how much storage do we need for the variable p1? Well, if you look at the definition of mystruct type, you will see that I need 1 byte for the member z. And I need 4 bytes each for the members x and y. Therefore, I need a total of 9 bytes of storage. Once again, this being a local variable of main, storage for this will be allocated in the activation record for main, which will be stored in the call stack, which resides in the stack segment. So, in the stack segment, I will have storage for p1. And in that storage, I will have a byte reserved for p1.z. And 4 bytes each reserved for p1.x and p1.y. Note that I am saying that this is the amount of memory allocated for p1, whereas p1.z required 1 byte here, p1.x and p1.y required 4 bytes at each of these places. So, there is still some gap here. So, if you are wondering what is that gap for padding, let us just wait for a few slides. But for the time being, let us note that since p1 and a are both local variables of main, therefore, memory for both p1 and a have been allocated in the activation record of main in the call stack, which resides in the stack segment. Now, in order to understand a little bit more about this gap or padding here, we must first ask what can we safely assume about the memory layout of structures, when I have variables of structure types in my program. So, here is my structure type, my struct type. And here is the variable. And here is the memory layout that we have already seen. What can we safely assume about this memory layout when programming in C++? Well, we must not make any assumptions about the relative layout of different members within the memory allocated for a structure. So, while it may be reasonable to assume that the memory for p1.z will be allocated immediately before the memory for p1.x and then the memory for p1.y, because they appeared in that order in the definition of my structure type. However, it is not safe to assume this in general when you are programming in C++. So, we will make no assumptions that p1.z appears before p1.x and that appears before p1.y. We will make no such assumptions about the layout of the structure in main memory. We are also not going to make any assumptions about whether there are paddings or gaps or unused memory locations after the locations allocated for different members of a structure. For example, we will not assume that there is no gap or unused memory location between the location allocated for p1.z and the first location allocated for p1.x. Similarly, we will not assume that there is no gap or padding or unused memory location between the last location allocated for p1.x and the first location allocated for p1.y. For example, this should also be a perfectly all right memory layout for my structure type as far as our program is concerned. So, we must not make any assumptions about the absence or presence of gaps or paddings between the memory locations allocated for different members of a structure. However, we can assume that the memory locations allocated for a particular member like p1.x are all going to be contiguous they are going to have consecutive addresses. So, for example, we can assume that p1.x will be stored in 4 contiguous locations and similarly p1.y will be stored in 4 contiguous locations. We cannot assume and we should not assume that there will be no gaps or paddings between p1.x and p1.y and similarly we must not assume that p1.x will be stored earlier in the address space than p1.y. Well, now that we know how structures are laid out in memory and what we can and cannot assume about them. Let us try to see how we are going to find out addresses of structures in memory and how we are going to use these addresses to access these structures in memory. Now, if you recall we had the ampersand and star operators which we had used with variables of basic data types. For example, here is a little program fragment and here int star basically refers to a pointer data type specifically pointer to an integer data type and in this assignment statement ampersand a is asking the computer to take the address of the starting location of variable a which is of type n since variable a is of type n we need 4 locations for storing the value of a. So, ampersand a will give us the address of the starting location of variable a and that will be assigned to pointer a and similarly when we say star pointer a we are telling the computer to look at the contents of the memory locations whose starting address is given by pointer a and we should look at these contents as an integer because pointer a is a pointer to an integer pointer a has been declared to be of the type pointer to integer. So, when I say star pointer a I am saying the contents as integer of the memory locations whose starting address is given by pointer a and those contents of those memory locations should be updated to the value 10 it turns out in C plus plus we can use the ampersand and star operators in exactly the same way with variables of structure types. So, here is a little C plus plus program fragment which uses the ampersand and star operator with structure types. So, here is our my structure type that we have seen earlier p 1 is a variable of structure type my structure type pointer p 1 is a variable which is of type pointer to my structure type. So, just like in star says it is a pointer to an integer. So, my structure type star says that it is a pointer to my structure type my structure type is a structure type and my structure type star is a pointer to that structure type. Similarly, when we say ampersand p 1 we are saying take the address of the starting location of variable p 1 which in this case happens to be of type my structure type and similarly, when we say star pointer p 1 we are asking the computer to update through this assignment statement the contents of the memory locations whose starting address is given by pointer p 1 and these contents should be thought of as having the value of a variable of type my structure type because pointer p 1 is declared to be of type pointer to my structure type. Therefore, star pointer p 1 should be treated like an object of type my structure type and we should treat the contents of the memory locations whose starting address is given by pointer p 1 as being the value of an object of type my structure type. So, in this case that object of type my structure type is being initialized to C 2 3 we have already seen initialization of structures earlier it means the member z of the object star pointer p 1 will be initialized to the character C and x and y the members x and y of the object star pointer p 1 will be initialized to 2 and 3 respectively. Now that we have seen how to find out addresses of structures and how to de-reference those addresses we want to ask can we access the member of a structure like p 1 dot x through a pointer to that structure like pointer p 1 well the answer to this is yes and by the obvious way since star pointer p 1 was allowing us to access the contents of the memory locations whose starting address was given by p 1 and these contents were being accessed as an object of type my structure type. So, I could simply say star pointer p 1 dot x and I will be accessing the member x of the object star pointer p 1. So, I could have this assignment statement in which I am accessing the member x of the object star pointer p 1 and updating it to 1 plus the value of member y of the object star pointer p 1 now in C plus plus the way to read this is that star pointer p 1 is an object of type my structure type. So, star pointer p 1 dot x is the member x of that object and C plus plus actually provides a separate operator for situations like this for example, in instead of saying star pointer p 1 dot x I could say pointer p 1 arrow x and instead of saying star pointer p 1 dot y I could say pointer p 1 arrow y. So, in general a pointer variable arrow member name is the same as star pointer variable basically dereference the pointer variable and find out the entire structure that the pointer variable is pointing to and then access member name of that structure. So, here is our original program in which I had initialized star pointer p 1 to C 2 3 and then added 1 to star pointer p 1 dot y and used it to update star pointer p 1 dot x. Here is a completely equivalent program where I have initialized the members z x and y of p 1 by using the arrow operator. So, pointer p 1 is assigned the address of p 1 and then I can use the arrow operator in this manner pointer p 1 arrow z is assigned C pointer p 1 arrow x is assigned to pointer p 1 arrow y is assigned 3 this will basically initialize p 1 to the values C for z 2 for x and 3 for y and then instead of using the star pointer p 1 dot x and star pointer p 1 dot y I could rewrite this assignment statement using the arrow operators like this. So, these are two completely functionally equivalent program fragments. So, in summary in this lecture we studied about pointers to variables of structure data types we saw the use of the ampersand and star operators with structures and we also saw the use of the arrow operator to access members of structures through pointers. Thank you.