CS50 Video Player
    • 🧁

    • 🍬

    • 🍌

    • 🍿
    • 0:00:00DOUG LLOYD: So at this point, we're pretty
    • 0:00:01familiar with all of the data types that come
    • 0:00:02native to C. We have characters, and floats, and integers, and doubles,
    • 0:00:08and we're also familiar now with the CS50 data types of strings and Booles.
    • 0:00:13But that doesn't limit everything that we can do.
    • 0:00:15We surely can do more.
    • 0:00:17Indeed, with structures, that gives us an ability
    • 0:00:19to start to define our own data types that
    • 0:00:21might be useful for our own programs.
    • 0:00:23What's cool about structures is they allow
    • 0:00:25us to unify many different variables of different data types
    • 0:00:29into a single brand new type.
    • 0:00:31And we can give that new type its own unique type
    • 0:00:34name as a way to identify it.
    • 0:00:36This isn't the first time we've seen the ability
    • 0:00:38to combine multiple variables together.
    • 0:00:40We've seen that with arrays.
    • 0:00:41Remember, the restriction with arrays is that we can only
    • 0:00:43combine things of the same type.
    • 0:00:45We can have a whole bunch of integers or a whole bunch of floats.
    • 0:00:47But we can't mix them up.
    • 0:00:49With structures, we actually can.
    • 0:00:51We can group together logical--
    • 0:00:53we can group together elements that have a logical connection to one another.
    • 0:00:56So for example, we can group together a structure
    • 0:00:58for say a student, where a student would have an ID
    • 0:01:01number, which is probably an integer.
    • 0:01:03But they also have a name, which is a string.
    • 0:01:05And they might have a GPA, which is a float.
    • 0:01:07And we can have all of these things tied together in basically
    • 0:01:10what's a super variable.
    • 0:01:11It's a variable that has other variables within it.
    • 0:01:15The way to do this in C is actually pretty straightforward.
    • 0:01:18Some syntax might look like this.
    • 0:01:20Instead of a student in this example, let's use a car.
    • 0:01:23We introduced the concept of a structure by saying, struct.
    • 0:01:26We're about to define a structure type.
    • 0:01:28And in particular, we're going to define a struct car.
    • 0:01:32And struct car actually now becomes our type name.
    • 0:01:35It's not just car.
    • 0:01:36It's struct car.
    • 0:01:38Inside of the curly braces from this point forward,
    • 0:01:40we can define all of the different variables
    • 0:01:42that we want inside of our super variable.
    • 0:01:45So for example, here are some things that are common to cars.
    • 0:01:48They have a year, the year they were manufactured.
    • 0:01:51We have a model, and a license plate, and an odometer, which
    • 0:01:55is a number of miles, and maybe they have an engine size,
    • 0:01:58which in the United States is usually represented
    • 0:02:00as a float for how many leaders the engine capacity is.
    • 0:02:04And notice that these are all different data types being mixed together.
    • 0:02:07We have integers, and we have two strings,
    • 0:02:09and we have a float, or a double all mixed up inside of one super variable.
    • 0:02:13To finish our definition of the structure,
    • 0:02:15we have a closing curly brace.
    • 0:02:16And then really important, a common syntax error
    • 0:02:19is a semicolon at the end, which completes
    • 0:02:21our definition of a struct car.
    • 0:02:25Once we define the structure, and usually we
    • 0:02:26define our structure at the very top of our program
    • 0:02:29up near our pound includes, and our pound defines,
    • 0:02:31but we also might define them in a separate dot H file
    • 0:02:34because maybe we're using this type definition in several different files,
    • 0:02:39or several different programs.
    • 0:02:40And so it makes sense to define it somewhere outside of one--
    • 0:02:44outside of a single file and have it be a dot H file that we can pound
    • 0:02:47include in multiple different contexts.
    • 0:02:50Now we have effectively created this new type and we can start to use it.
    • 0:02:54And we can create variables of this type just like we
    • 0:02:56can create variables of any other type.
    • 0:02:58Int x, that's how we create an integer called x.
    • 0:03:00Struct car y, that's how we create a variable called y of type struct car.
    • 0:03:06So that's how we create the variable overall.
    • 0:03:08How do we create the individual fields, or members of that?
    • 0:03:12Or rather, how do we access the individual fields or members
    • 0:03:15of that structure?
    • 0:03:16We can do so using something called the dot operator.
    • 0:03:19Let's take a look at what that looks like.
    • 0:03:21So the top here, I have a variable declaration.
    • 0:03:23Struct car, my car.
    • 0:03:25Again, here the type is struct car.
    • 0:03:28And the variable name is my car.
    • 0:03:31From this point forward, whenever I want to refer to a field,
    • 0:03:35or a member within my car, which is again, the variable name,
    • 0:03:39I can use the dot operator to access those individual fields within my car.
    • 0:03:43So I can say, for example, mycar.year equals 2011.
    • 0:03:47I can't just say year equals 2011.
    • 0:03:49Year is something that is part of my car.
    • 0:03:52So I have to always refer to it in the context of my car.
    • 0:03:56I can say strcpy(mycar.plate, "CS50").
    • 0:04:00Remember, it's a string.
    • 0:04:01I can't just assign it.
    • 0:04:02I have to copy that string into the variable.
    • 0:04:06I can say, mycar.odometer equals 50505 or anything else that I want to do.
    • 0:04:11I can set the engine size, and I can set the model.
    • 0:04:14I can do whatever else I want to do just by accessing
    • 0:04:18the fields similar to this.
    • 0:04:21But structures like variables of all other data types,
    • 0:04:23we don't have to just use the stack for this.
    • 0:04:25We don't just have to say struct car, my car, semicolon.
    • 0:04:29We could dynamically allocate this if we don't
    • 0:04:31know at the beginning of our program, for example,
    • 0:04:33that we're going to need a certain number of these things.
    • 0:04:36We can just declare this on the fly dynamically using pointers of course.
    • 0:04:41In order to access our fields in that situation,
    • 0:04:43we don't use just the dot operator because we
    • 0:04:46have a pointer to a structure.
    • 0:04:47We also have to first, as you may recall, dereference that pointer
    • 0:04:50and then access its fields.
    • 0:04:52It adds a little bit of extra stuff, but let's take a look again here.
    • 0:04:55So here now, instead of statically declaring a struct car called my car,
    • 0:05:00I'm going to dynamically allocate a struct car called my car.
    • 0:05:04So struct car star my car, and then I'm going to malloc space for a struct car.
    • 0:05:10And here's a cool thing about size of.
    • 0:05:12Size of is not just built in.
    • 0:05:14It doesn't just happen to know just the size of integers, characters, floats,
    • 0:05:17and doubles.
    • 0:05:18It can also figure out on the fly exactly how much space
    • 0:05:21is required for a struct car.
    • 0:05:22So you don't have to go through and figure out, OK, well, this
    • 0:05:24is probably like 30 bytes or something.
    • 0:05:26You can just say, size of struct car, and let
    • 0:05:28the computer figure it out for you.
    • 0:05:30So struct car star my car equals malloc size of struct car.
    • 0:05:34That dynamically allocates on the heap one chunk of memory large enough
    • 0:05:39to hold a single struct car within it.
    • 0:05:42And then I can access my fields by first dereferencing the pointer.
    • 0:05:47And then once I've dereferenced the pointer,
    • 0:05:49I can then use the dot operator to access the fields.
    • 0:05:52Again, it's very similar to what we just saw.
    • 0:05:54But the syntax here is a little cumbersome.
    • 0:05:56Now we have extra parentheses, we have this star, we have this dot,
    • 0:05:59surely there's got to be an easier way.
    • 0:06:01C programmers love for there to be easier ways to do things.
    • 0:06:04And in fact, there is a shortcut for this.
    • 0:06:07It so happens that accessing the field of a structure via its pointer
    • 0:06:12is a common enough operation that there is an entirely different operator that
    • 0:06:16allows us to do this much more succinctly.
    • 0:06:19And it is called the arrow operator, which
    • 0:06:20is a hyphen and then a greater than symbol,
    • 0:06:23literally making it look like it's an arrow.
    • 0:06:26It does two operations back to back.
    • 0:06:29So for the first thing it does is it dereferences the pointer, which
    • 0:06:32is on the left of the arrow.
    • 0:06:34And then it's going to access the field, which is on the right of the arrow.
    • 0:06:38So for example, this is what the code looked like before.
    • 0:06:41This is what we just had on the slide a second ago
    • 0:06:43where I'm dereferencing pointers.
    • 0:06:45And then I'm using the dot operator to access the fields.
    • 0:06:49Here is what the same code would look like with the arrow syntax.
    • 0:06:52And the same thing is happening here.
    • 0:06:53The first thing I'm doing is I'm dereferencing my car.
    • 0:06:56But the arrow operator does that for me.
    • 0:06:58I don't have to use the star syntax.
    • 0:06:59It just knows if I have an arrow there, I
    • 0:07:01need to first dereference the thing on the left,
    • 0:07:04and then I can access the field on the right.
    • 0:07:07So using the arrow operator is a great way
    • 0:07:09to have a shorthand for accessing the field of a structure to which you only
    • 0:07:13have a pointer.
    • 0:07:14And you'll probably use this a fair amount.
    • 0:07:17So it's syntax definitely to befriend and to get used to.
    • 0:07:21I'm Doug Lloyd.
    • 0:07:22This is CS50.
  • CS50.ai
Shortcuts
Before using a shortcut, click at least once on the video itself (to give it "focus") after closing this window.
Play/Pause spacebar or k
Rewind 10 seconds left arrow or j
Fast forward 10 seconds right arrow or l
Previous frame (while paused) ,
Next frame (while paused) .
Decrease playback rate <
Increase playback rate >
Toggle captions on/off c
Toggle mute m
Toggle full screen f or double-click video