Video instructions and help with filling out and completing Who Form 2220 Defined

Instructions and Help about Who Form 2220 Defined

Hello and welcome back this is the start of a major new subject in the language called Struck's structs are a user-defined data type in C so up until now we've been using the primitive data types that are defined as part of the language the intz and floats and doubles and now we're going to learn a way of making our own data types a user-defined data type and in this segment it will be short and just show you how to do it and then in the subsequent segments we'll we will exercise it more and put it into practice so this one will be short and I'm expecting you to have to come back and watch this one again that's kind of how major new subjects work in in all areas so let's have a look now at user-defined types or structs in C okay let's start off by motivating the need to have such a thing as a struct in the C language suppose we're going to write a program where we want to represent a date and we could declare three int variables a day a month and a year so three variables are one thing and if we want to pass this one thing into a function we would wind up passing three parameters the day the month and in a year so what we need to be able to do is to associate these three things as component parts of one thing so add a month in a year is a date that's one thing so we're going to put them together using a syntax in the language that would allow us to create a new data type that is composed of a day a month and a year so I talked about this fairly often that this is the foundation of object-oriented programming we're gonna put things together making one thing and I I don't want to call it an object because that word is reserved for discussions of object-oriented programming but we are going to associate component parts into one other thing well creating structs in the C language there are different approaches to it and I want to keep them separate by calling them a name so we're gonna have approach one two and three so here's approach one the keyword is struct and then we use braces to encapsulate the three component parts of this data type so our new data type consists of a day a month in a year and this is the definition of the datatype itself so struct and then the open brace and the close brace this is the datatype and in this one it's the datatype followed by the name of a variable so the variable is my birthday and the datatype is all of this this keyword struct from here to here same as if we said aunt I this would be the aunt and this would be the I okay this is not very useful way of using structs because you can't reuse it we can make one variable of that new data type but then this data type that we created this data type that we created doesn't have a name it's not stored anywhere to be reused so this approach one is the simplest possible one where you create something that you don't need anymore and it's not it's not really useful but it's the first way that we look at it so here's approach to it starts to be more useful we have the keyword struct and then we have a name for our data type so the name of the data type in this example this is approach to the name of the data type is struct date if we want to declare variables of our new data type we say struct date and then we can say birthday anniversary New Year's Day and each of these is a variable of that data type so struct date and there's a month a day in a year so what does it mean that this variable birthday has data type struct date it means here's a picture of it over here this thing that we've created called birthday has three parts to it a month a day and a year so now we're going to introduce the dot operator that allows us to have access to the component parts of that thing I'm trying not to say that object so the dot operator I'd like you to read as apostrophe s or ownership so we would say birthdays month gets 11 and birthdays birthdays day gets 16 birthdays year gets 1980 so what is the datatype of birthdays year it's an int and the datatype of birthdays day is an INT and so on the datatype of birthday is a struct date so by declaring one thing we have actually declared three-inch because of the definition of that new data type here's approach 3 and this one is the most useful and the one that we will be using most often in the subsequent video segments so we're going to use the keyword type def and we're going to define a new data type so type def struct month day and year is the data type and then date in all caps is going to be the name of our new data type so a couple of things happened here remember an approach one we had struct and then the month day and year that was the data type so now we're kind of using approach one and we're using a type def with it so type def means I want to give a name to whatever is following type def this data type to be the name date now when we want to declare variables of this new data type we can just use the name of the data type my convention is to use all caps

100%
Loading, please wait...