All this business with struct this and enum that is getting annoying, right? Why should you have to stick struct or enum before the name of your data type? Well, one reason is that you can easily have both a struct x and an enum x without C getting confused. But it's still annoying. This is why you can name your own data types using the typedef keyword:

typedef old_data_type new_name_for_it;
 
typedef struct ceo_member ceo_member_t;
 
typedef enum Machine machine_t;
 
typedef short unsigned int nice_int_t;

This example assumes you have previously defined struct ceo_member and enum Machine. After these typedefs, you can declare variables by simply writing something like ceo_member_t one_member or machine_t my_oric. The _t at the end of every typedef name isn't mandatory, but is generally a good idea so that you can tell what sort of data type this is. As you can see from the third example, you can also give new names to simple C data types.

Tags: 


Add new comment

Leave a comment