hey everyone Professor egg here and today we're going to talk about structures and pointers so let's go ahead and get started shall we so we'll get started by having a structure and we'll just give it a couple of integers okay and then what we'll do is we'll create a foo variable and we'll initialize it with I don't know eight and three so now what we'll do is we'll create a pointer and remember a pointer is just a variable that holds a memory address so we've used integer pointers double pointers float pointers in the past now we're just going to use a foo pointer so same kind of thing we're going to need the data type we're going to need the asterisk and then we're going to need the name of our pointer okay and we can use the address operator with structures so once we've done that we can now access F through p all right in the same kind of ways that we've done it before you know you might think that if we wanted to display the contents of the expert variable right so if we were to use the plain old Foo variable right do something like this you know no problem you know we're going to see eight appear on the screen right because we assigned 8 to X we initialized that x with a that member okay now you might be thinking well let's do the exact same thing we'll just do it with the pointer this time we'll dereference the pointer and then we'll access the X so you can kind of see that we've already got a squiggle under here and this isn't going to work right why why not okay it's because of the order of operations so the dot operator has a higher order right it has higher precedence dot has precedence over the dereference operator so what this is saying right here is this is saying dereference what's in x right so this right here has priority so you're extracting the eight from the X and then trying to de-reference eight which doesn't make any sense right because you can't de-reference an integer you're having to dereference or you have to reference a member address so if we wanted to make this work then what we'd have to do is we'd have to change the order of operations okay so how are we going to change order of operations we're just going to put our parentheses around that instead so now what we're doing is we're saying go to the memory location whose address is in p right whose address is in P for f so then we now have access to the F structure and then we can access its X member okay so you're going to see that we're going to see eight and it's going to work just fine now this is a little awkward and so as an alternative to this we can use this this arrow-like notation okay and what it is is it's hyphen and then angle bracket okay and then you put the member that you want so this is equivalent to what we have above okay this is equivalent to store P dot X same means the exact same thing it's just syntactically a little bit cleaner and the design of this operator this little arrow operator here is on purpose it's to make it look as if you know you're having a pointer right that you're working with the pointer because it's an arrow that's pointing somewhere right okay and then we can also get our y out of there too just just for completeness okay so you can see that you know this is going to work just fine okay so you can do either right most people will use the arrow operator I mean it's kind of unusual to see this kind of code somewhere but you can't do it right it means the exact same thing as this these are these are equivalent okay so let's take a look now at dynamic memory allocation okay so let us dynamically allocate a foo structure all right so I'll show you a couple different ways that you can do that so remember we can create a food pointer which we will do and then if we want to dynamically allocate a foo we dynamically allocate it in the exact same way we would um any other primitive data type right so you could say new int for example right we know how to do that instead of saying new it we're just going to say new Foo that's it and then this is going to create a new unnamed Foo variable in memory and new is going to return its memory address so we're going to have to put that memory address somewhere where we're going to put it we're going to put it in a variable that holds a memory address what kind of variable what kind of memory address a flu variable right so now we've got that and let's not forget that for every new that executes you have to have a delete statement so we're going to delete when we're done with it okay and then once we have this right p is now pointing to the foo right the unnamed Foo variable so if I want to assign to X something what do I do I use P to go to the memory location where the foo variable is stored and then I'm going to access its X member and I'm going to assign it a value and then I'm going to do the same thing for its y and then I will see out both of those okay and remember you can do this if you want it's kind of weird but you can do it right um it's possible it's not wrong uh let's see here okay and so then we will oops I'll layer here my uh stream inspiration operator so we'll go ahead and um test that and you're going to see that it's going to work just fine okay so that's how you can dynamically allocate a structure and don't forget you always have to free up the memory used by your structure when you're done with it to avoid member leaks all right now that is dynamically allocating a food structure and let me show you one more thing here too you can use initialization lists right with this dynamically allocated Foo as well all you have to do is put your initialization list right after the foo here and then you can put whatever values you want in there okay and so that will that will work right so then you'll see the three and the nine so you can use initialization lists with dynamically allocated um food structures Trader Dynamic allocate structure so you can use initialization lists all right so let us now see what it looks like to dynamically allocate a array okay so let's do that so remember if you were if we were doing a dynamically allocated uh Ray of integers we would have something that looks like maybe this well we're going to use summer syntax it's just that instead of dynamically allocating an array of integers we're going to dynamically allocate an array of Foos okay so we're going to have three foods and we're going to delete our array when we're done with it don't forget since it's an array you have to have those square brackets and then uh we're going to specify the pointer that contains the memory address for the array that we want to delete so we've got that once our array is created then we can access it just like any other array so I can say n0 dot x equals three and n 0 dot y equals two fine I could also asterisk n plus one dot X and then you ought to complete dot X um equals seven and star uh n plus one Dot y equals four but remember what we said about the order of operations what this is actually trying to do is it's trying to the reference this x here so I would have to override that order of operations just like before see that and see how gross that is yuck yuck yuck yuck yuck right um You can do it but it's disgusting what's cleaner is just to use the plain old array notation right so n 2 dot x equals zero and n 2 dot y equals um one okay once we've done that then we can use ourselves a little for Loop here to go through and print out the contents of the array okay so we'll do c out in of I dot X and and have I dot y okay and then that will print out the contents contents of our array okay and there we go there's our struct again just in case you forget what it looked like okay so let's go ahead and uh run this so you can see there's the contents of our array right and just like before right you can use initialization lists here as well right so we're going to have three elements so we'll have three initialization lists inside of our initialization list with initialization list of initialization list right this is going to be for the first element this will be for the second full element this will be for the third full element so we'll just do one two we'll do three four and then five six and uh I'll get rid of this code here so it's not interfering so the output doesn't get in the way okay so let's try this okay so you can see that that worked just fine all right so there you go now you know how to use pointers and structures okay so that's going to bring this video to a close if you're a student of mine you have questions about any of the topics that were covered in this video feel free to drop me an email stop by my office hours or hit me up on Zoom online for the rest of you if you thought the video was useful please consider giving a thumbs up I thought the videos sucked you got the thumbs down button as well consider supporting the channel in various ways you can subscribe you can join as a member with additional perks for as little as 99 Cents leave a comment whatever but most of all thanks for watching and we'll see you next time