Defining Custom Types

DOUG LLOYD: So if you’ve
seen our video on structures, you already know that we have the
ability to define our own data types. But if you’ve used structures, you know
that sometimes using those data types can actually be a little
cumbersome because we have to use the struct keyword
when we’re working with them– struct car or struct student. That’s a lot to type
if we just want to have something simple, like a structure. We want to have– we want to
be able to work with things a little more shorthand way,
like int or char, something a lot more convenient to type.>>Fortunately, there’s
a way to do this in C, with something called typedef, which is
a way to create shorthand or rewritten names for data types. You can rewrite data
types that already exist, or you can write data types of your own.>>The first thing you do is you
define a type in the normal way, and then you just alias it to something
else– typedef old name new name. Pretty straightforward, right? So we could typedef for example,
the already existing data type of unsigned char as byte. And then, from now on, after we’ve
made this type definition– again, which is usually going to be at the top
of our dot C files, or in a dot H file separately, we can just
use bite everywhere we would have used unsigned char. That’s a lot shorter to type.>>In CS50’s library, we do this–
we typedef char star as string. Because we abstracted
away the idea of pointers. And the fact that a string is really
a pointer to the first character– an array of characters,
because it’s just so confusing to get your head around
that– but in fact that’s what we do. And this line of code actually exists
in CS50 dot H typedef char star string, just to make that a little less
cumbersome to have to deal with. Already you’ve seen probably a
little bit of the value here, but typedef becomes great
when combined with structures, as I alluded to earlier. Structures have a two-word type name. And so they can be really annoying
to create variables of that type, or [INAUDIBLE] of struct something, your
lines can just get really, really long. And so you can use typedef to come
up with something a lot shorter.>>So if I define a car as follows, I have
my definition of a car– struct car, and then open curly brace, all
the fields of my structure, close curly brace, semi-colon–
after I’ve defined my data type, I can typedef struct
car as car underscore t. And then when I’ve done that, now every
time I would have otherwise used struct car, I can just use car underscore t. That’s a lot shorter of a way to
express this idea of this structure that I just created.>>Alternatively, because structures
are so commonly used in typedef, there’s a– you can actually define the
type in between the start and the end. So the typedef structure again is
usually typedef, old name, new name, where the names are already
types that you’ve created. But you can actually define a structure
right in the middle of the typedef instead of having to define
it separately, and then do a typedef of it. And that would look just like
this– typedef struct car, open curly brace, all of your field
definitions, close curly brace, car t. So the old name is all that stuff in the
red, you’re just defining the structure and naming it at the same time. And then the new name
is car underscore t. And so if we start to use
this in code, previously I might have said, struct
car mycar semi-colon. Well I don’t have to do that anymore. Now that I’ve used the typedef, I
can just say car underscore t, mycar. That’s a lot shorter of a way
to do it, and in fact, it’s going to be a lot more
convenient, especially as you start to use structures
a lot more in your code.>>I’m Doug Lloyd. This is CS50.

One Comment

Add a Comment

Your email address will not be published. Required fields are marked *