

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Difficulty: 3
click for more info
Not enough gems
Cost: 6 gems
1: Structs
incomplete
2: Initializers
incomplete
3: Scaling Coordinate
incomplete
4: Typedef
incomplete
5: Sizeof
incomplete
6: Struct Padding
incomplete
This lesson's interactive features are locked, please to keep using them
By now, you're probably tired of typing struct Coordinate over and over again, and you're wondering "How can I make my struct types easier to write, like int?"
Good news! C can do this with the typedef keyword.
struct Pastry {
char *name;
float weight;
};
This can also be written as:
typedef struct Pastry {
char *name;
float weight;
} pastry_t;
Now, you can use pastry_t wherever before you would have used struct Pastry.
The _t at the end is a common convention to indicate a type.
In fact, you can optionally skip giving the struct a name:
typedef struct {
char *name;
float weight;
} pastry_t;
pastry_t muffin = {"Muffin", 0.3};
In this case you'd only be able to refer to the type as pastry_t. In general, I do give the struct an actual name (e.g. Pastry), but I only use the typedef'd type. We'll be using this convention in this course.