Alias a Record Type with typedef
Several kits use the same record shape. The struct declaration below defines that shape without a struct tag. typedef gives the type the short name KitRecord. An alias is another name, not a new incompatible type. It does not create a kit; KitRecord kit creates an object of that type. Access its fields with dot notation.
#include <stdio.h>
typedef struct {
const char *name;
int count;
} KitRecord;
int main(void) {
KitRecord kit = {"Brushes", 2};
printf("%s: %d\n", kit.name, kit.count);
return 0;
}
Run and check Brushes: 2. KitRecord is a type name; kit is an object with storage for its members. Its name member holds a pointer to the string literal, not a separate copy of the characters. Changing the object's count would not change the type.
You can also alias a type you already know: typedef int Count; lets you declare Count total = 3;. A typedef does not reserve memory until you declare an object. In the next section, these record types will also describe objects in dynamically allocated storage.